1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * EffecTV:
5  * Copyright (C) 2001 FUKUCHI Kentarou
6  *
7  * Inspired by Adrian Likin's script for the GIMP.
8  * EffecTV is free software.  This library is free software;
9  * you can redistribute it and/or
10  *  modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 /**
26  * SECTION:element-shagadelictv
27  *
28  * Oh behave, ShagedelicTV makes images shagadelic!
29  *
30  * <refsect2>
31  * <title>Example launch line</title>
32  * |[
33  * gst-launch-1.0 -v videotestsrc ! shagadelictv ! videoconvert ! autovideosink
34  * ]| This pipeline shows the effect of shagadelictv on a test stream.
35  * </refsect2>
36  */
37 
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 
42 #include <math.h>
43 #include <string.h>
44 
45 #include "gstshagadelic.h"
46 #include "gsteffectv.h"
47 
48 #ifndef M_PI
49 #define M_PI  3.14159265358979323846
50 #endif
51 
52 #define gst_shagadelictv_parent_class parent_class
53 G_DEFINE_TYPE (GstShagadelicTV, gst_shagadelictv, GST_TYPE_VIDEO_FILTER);
54 
55 static void gst_shagadelic_initialize (GstShagadelicTV * filter,
56     GstVideoInfo * in_info);
57 
58 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
59 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("BGRx")
60 #else
61 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("xRGB")
62 #endif
63 
64 static GstStaticPadTemplate gst_shagadelictv_src_template =
65 GST_STATIC_PAD_TEMPLATE ("src",
66     GST_PAD_SRC,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS (CAPS_STR)
69     );
70 
71 static GstStaticPadTemplate gst_shagadelictv_sink_template =
72 GST_STATIC_PAD_TEMPLATE ("sink",
73     GST_PAD_SINK,
74     GST_PAD_ALWAYS,
75     GST_STATIC_CAPS (CAPS_STR)
76     );
77 
78 static gboolean
gst_shagadelictv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)79 gst_shagadelictv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
80     GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
81 {
82   GstShagadelicTV *filter = GST_SHAGADELICTV (vfilter);
83   gint width, height, area;
84 
85   width = GST_VIDEO_INFO_WIDTH (in_info);
86   height = GST_VIDEO_INFO_HEIGHT (in_info);
87 
88   area = width * height;
89 
90   g_free (filter->ripple);
91   g_free (filter->spiral);
92   filter->ripple = (guint8 *) g_malloc (area * 4);
93   filter->spiral = (guint8 *) g_malloc (area);
94 
95   gst_shagadelic_initialize (filter, in_info);
96 
97   return TRUE;
98 }
99 
100 static void
gst_shagadelic_initialize(GstShagadelicTV * filter,GstVideoInfo * info)101 gst_shagadelic_initialize (GstShagadelicTV * filter, GstVideoInfo * info)
102 {
103   int i, x, y;
104 #ifdef PS2
105   float xx, yy;
106 #else
107   double xx, yy;
108 #endif
109   gint width, height;
110 
111   width = GST_VIDEO_INFO_WIDTH (info);
112   height = GST_VIDEO_INFO_HEIGHT (info);
113 
114   i = 0;
115   for (y = 0; y < height * 2; y++) {
116     yy = y - height;
117     yy *= yy;
118 
119     for (x = 0; x < width * 2; x++) {
120       xx = x - width;
121 #ifdef PS2
122       filter->ripple[i++] = ((unsigned int) (sqrtf (xx * xx + yy) * 8)) & 255;
123 #else
124       filter->ripple[i++] = ((unsigned int) (sqrt (xx * xx + yy) * 8)) & 255;
125 #endif
126     }
127   }
128 
129   i = 0;
130   for (y = 0; y < height; y++) {
131     yy = y - height / 2;
132 
133     for (x = 0; x < width; x++) {
134       xx = x - width / 2;
135 #ifdef PS2
136       filter->spiral[i++] = ((unsigned int)
137           ((atan2f (xx,
138                       yy) / ((float) M_PI) * 256 * 9) + (sqrtf (xx * xx +
139                       yy * yy) * 5))) & 255;
140 #else
141       filter->spiral[i++] = ((unsigned int)
142           ((atan2 (xx, yy) / M_PI * 256 * 9) + (sqrt (xx * xx +
143                       yy * yy) * 5))) & 255;
144 #endif
145 /* Here is another Swinger!
146  * ((atan2(xx, yy)/M_PI*256) + (sqrt(xx*xx+yy*yy)*10))&255;
147  */
148     }
149   }
150   filter->rx = fastrand () % width;
151   filter->ry = fastrand () % height;
152   filter->bx = fastrand () % width;
153   filter->by = fastrand () % height;
154   filter->rvx = -2;
155   filter->rvy = -2;
156   filter->bvx = 2;
157   filter->bvy = 2;
158   filter->phase = 0;
159 }
160 
161 static GstFlowReturn
gst_shagadelictv_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)162 gst_shagadelictv_transform_frame (GstVideoFilter * vfilter,
163     GstVideoFrame * in_frame, GstVideoFrame * out_frame)
164 {
165   GstShagadelicTV *filter = GST_SHAGADELICTV (vfilter);
166   guint32 *src, *dest;
167   gint x, y;
168   guint32 v;
169   guint8 r, g, b;
170   gint width, height;
171 
172   src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
173   dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
174 
175   width = GST_VIDEO_FRAME_WIDTH (in_frame);
176   height = GST_VIDEO_FRAME_HEIGHT (in_frame);
177 
178   for (y = 0; y < height; y++) {
179     for (x = 0; x < width; x++) {
180       v = *src++ | 0x1010100;
181       v = (v - 0x707060) & 0x1010100;
182       v -= v >> 8;
183 /* Try another Babe!
184  * v = *src++;
185  * *dest++ = v & ((r<<16)|(g<<8)|b);
186  */
187       r = ((gint8) (filter->ripple[(filter->ry + y) * width * 2 + filter->rx +
188                   x] + filter->phase * 2)) >> 7;
189       g = ((gint8) (filter->spiral[y * width + x] + filter->phase * 3)) >> 7;
190       b = ((gint8) (filter->ripple[(filter->by + y) * width * 2 + filter->bx +
191                   x] - filter->phase)) >> 7;
192       *dest++ = v & ((r << 16) | (g << 8) | b);
193     }
194   }
195 
196   filter->phase -= 8;
197   if ((filter->rx + filter->rvx) < 0 || (filter->rx + filter->rvx) >= width)
198     filter->rvx = -filter->rvx;
199   if ((filter->ry + filter->rvy) < 0 || (filter->ry + filter->rvy) >= height)
200     filter->rvy = -filter->rvy;
201   if ((filter->bx + filter->bvx) < 0 || (filter->bx + filter->bvx) >= width)
202     filter->bvx = -filter->bvx;
203   if ((filter->by + filter->bvy) < 0 || (filter->by + filter->bvy) >= height)
204     filter->bvy = -filter->bvy;
205   filter->rx += filter->rvx;
206   filter->ry += filter->rvy;
207   filter->bx += filter->bvx;
208   filter->by += filter->bvy;
209 
210   return GST_FLOW_OK;
211 }
212 
213 static void
gst_shagadelictv_finalize(GObject * object)214 gst_shagadelictv_finalize (GObject * object)
215 {
216   GstShagadelicTV *filter = GST_SHAGADELICTV (object);
217 
218   g_free (filter->ripple);
219   filter->ripple = NULL;
220 
221   g_free (filter->spiral);
222   filter->spiral = NULL;
223 
224   G_OBJECT_CLASS (parent_class)->finalize (object);
225 }
226 
227 static void
gst_shagadelictv_class_init(GstShagadelicTVClass * klass)228 gst_shagadelictv_class_init (GstShagadelicTVClass * klass)
229 {
230   GObjectClass *gobject_class = (GObjectClass *) klass;
231   GstElementClass *gstelement_class = (GstElementClass *) klass;
232   GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
233 
234   gobject_class->finalize = gst_shagadelictv_finalize;
235 
236   gst_element_class_set_static_metadata (gstelement_class, "ShagadelicTV",
237       "Filter/Effect/Video",
238       "Oh behave, ShagedelicTV makes images shagadelic!",
239       "Wim Taymans <wim.taymans@chello.be>");
240 
241   gst_element_class_add_static_pad_template (gstelement_class,
242       &gst_shagadelictv_sink_template);
243   gst_element_class_add_static_pad_template (gstelement_class,
244       &gst_shagadelictv_src_template);
245 
246   vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_shagadelictv_set_info);
247   vfilter_class->transform_frame =
248       GST_DEBUG_FUNCPTR (gst_shagadelictv_transform_frame);
249 }
250 
251 static void
gst_shagadelictv_init(GstShagadelicTV * filter)252 gst_shagadelictv_init (GstShagadelicTV * filter)
253 {
254   filter->ripple = NULL;
255   filter->spiral = NULL;
256 }
257