429a9b1c62f85bc40429851d546a6e9533560e36
[platform/upstream/gstreamer.git] / gst / effectv / gstrev.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * EffecTV:
5  * Copyright (C) 2001 FUKUCHI Kentarou
6  *
7  * EffecTV - Realtime Digital Video Effector
8  * Copyright (C) 2001 FUKUCHI Kentarou
9  *
10  * revTV based on Rutt-Etra Video Synthesizer 1974?
11
12  * (c)2002 Ed Tannenbaum
13  *
14  * This effect acts like a waveform monitor on each line.
15  * It was originally done by deflecting the electron beam on a monitor using
16  * additional electromagnets on the yoke of a b/w CRT. 
17  * Here it is emulated digitally.
18
19  * Experimaental tapes were made with this system by Bill and 
20  * Louise Etra and Woody and Steina Vasulka
21
22  * The line spacing can be controlled using the 1 and 2 Keys.
23  * The gain is controlled using the 3 and 4 keys.
24  * The update rate is controlled using the 0 and - keys.
25  
26  * EffecTV is free software. This library is free software;
27  * you can redistribute it and/or
28  * modify it under the terms of the GNU Library General Public
29  * License as published by the Free Software Foundation; either
30  * version 2 of the License, or (at your option) any later version.
31  *
32  * This library is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35  * Library General Public License for more details.
36  *
37  * You should have received a copy of the GNU Library General Public
38  * License along with this library; if not, write to the
39  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
40  * Boston, MA 02111-1307, USA.
41  */
42
43 #ifdef HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46 #include <math.h>
47 #include <string.h>
48 #include <gst/gst.h>
49 #include <gstvideofilter.h>
50
51 #define GST_TYPE_REVTV \
52   (gst_revtv_get_type())
53 #define GST_REVTV(obj) \
54   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_REVTV,GstRevTV))
55 #define GST_REVTV_CLASS(klass) \
56   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_REVTV,GstRevTVClass))
57 #define GST_IS_REVTV(obj) \
58   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_REVTV))
59 #define GST_IS_REVTV_CLASS(obj) \
60   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_REVTV))
61
62 #define THE_COLOR 0xffffffff
63
64 typedef struct _GstRevTV GstRevTV;
65 typedef struct _GstRevTVClass GstRevTVClass;
66
67 struct _GstRevTV
68 {
69   GstVideofilter videofilter;
70
71   gint width, height;
72   gint vgrabtime;
73   gint vgrab;
74   gint linespace;
75   gint vscale;
76 };
77
78 struct _GstRevTVClass
79 {
80   GstVideofilterClass parent_class;
81
82   void (*reset) (GstElement * element);
83 };
84
85 /* Filter signals and args */
86 enum
87 {
88   /* FILL ME */
89   LAST_SIGNAL
90 };
91
92 enum
93 {
94   ARG_0,
95   ARG_DELAY,
96   ARG_LINESPACE,
97   ARG_GAIN
98 };
99
100 static void gst_revtv_base_init (gpointer g_class);
101 static void gst_revtv_class_init (gpointer g_class, gpointer class_data);
102 static void gst_revtv_init (GTypeInstance * instance, gpointer g_class);
103
104 static void gst_revtv_set_property (GObject * object, guint prop_id,
105     const GValue * value, GParamSpec * pspec);
106 static void gst_revtv_get_property (GObject * object, guint prop_id,
107     GValue * value, GParamSpec * pspec);
108 static void gst_revtv_setup (GstVideofilter * videofilter);
109 static void gst_revtv_rgb32 (GstVideofilter * videofilter, void *d, void *s);
110
111 /* static guint gst_revtv_signals[LAST_SIGNAL] = { 0 }; */
112
113 GType
114 gst_revtv_get_type (void)
115 {
116   static GType revtv_type = 0;
117
118   if (!revtv_type) {
119     static const GTypeInfo revtv_info = {
120       sizeof (GstRevTVClass),
121       gst_revtv_base_init,
122       NULL,
123       (GClassInitFunc) gst_revtv_class_init,
124       NULL,
125       NULL,
126       sizeof (GstRevTV),
127       0,
128       (GInstanceInitFunc) gst_revtv_init,
129     };
130
131     revtv_type =
132         g_type_register_static (GST_TYPE_VIDEOFILTER, "GstRevTV", &revtv_info,
133         0);
134   }
135   return revtv_type;
136 }
137
138 static GstVideofilterFormat gst_revtv_formats[] = {
139   {"RGB ", 32, gst_revtv_rgb32, 24, G_BIG_ENDIAN, 0x0000ff00, 0x00ff0000,
140       0xff000000}
141 };
142
143 static void
144 gst_revtv_base_init (gpointer g_class)
145 {
146   /* elementfactory information */
147   static GstElementDetails gst_revtv_details = GST_ELEMENT_DETAILS ("RevTV",
148       "Filter/Effect/Video",
149       "A video waveform monitor for each line of video processed",
150       "Wim Taymans <wim.taymans@chello.be>");
151
152   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
153   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
154   int i;
155
156   gst_element_class_set_details (element_class, &gst_revtv_details);
157
158   for (i = 0; i < G_N_ELEMENTS (gst_revtv_formats); i++) {
159     gst_videofilter_class_add_format (videofilter_class, gst_revtv_formats + i);
160   }
161
162   gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
163 }
164
165 static void
166 gst_revtv_class_init (gpointer klass, gpointer class_data)
167 {
168   GObjectClass *gobject_class;
169   GstVideofilterClass *videofilter_class;
170
171   gobject_class = G_OBJECT_CLASS (klass);
172   videofilter_class = GST_VIDEOFILTER_CLASS (klass);
173
174   gobject_class->set_property = gst_revtv_set_property;
175   gobject_class->get_property = gst_revtv_get_property;
176
177   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DELAY,
178       g_param_spec_int ("delay", "Delay", "Delay in frames between updates",
179           1, 100, 1, G_PARAM_READWRITE));
180   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LINESPACE,
181       g_param_spec_int ("linespace", "Linespace", "Control line spacing",
182           1, 100, 6, G_PARAM_READWRITE));
183   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GAIN,
184       g_param_spec_int ("gain", "Gain", "Control gain",
185           1, 200, 50, G_PARAM_READWRITE));
186
187   videofilter_class->setup = gst_revtv_setup;
188 }
189
190 static void
191 gst_revtv_init (GTypeInstance * instance, gpointer g_class)
192 {
193   GstRevTV *restv = GST_REVTV (instance);
194
195   restv->vgrabtime = 1;
196   restv->vgrab = 0;
197   restv->linespace = 6;
198   restv->vscale = 50;
199 }
200
201 static void
202 gst_revtv_setup (GstVideofilter * videofilter)
203 {
204   GstRevTV *revtv;
205
206   g_return_if_fail (GST_IS_REVTV (videofilter));
207   revtv = GST_REVTV (videofilter);
208
209   revtv->width = gst_videofilter_get_input_width (videofilter);
210   revtv->height = gst_videofilter_get_input_height (videofilter);
211 }
212
213 static void
214 gst_revtv_rgb32 (GstVideofilter * videofilter, void *d, void *s)
215 {
216   GstRevTV *filter;
217   guint32 *src, *dest;
218   gint width, height;
219   guint32 *nsrc;
220   gint y, x, R, G, B, yval;
221
222   filter = GST_REVTV (videofilter);
223
224   src = (guint32 *) s;
225   dest = (guint32 *) d;
226
227   width = filter->width;
228   height = filter->height;
229
230   /* Clear everything to black */
231   memset (dest, 0, width * height * sizeof (guint32));
232
233   // draw the offset lines
234   for (y = 0; y < height; y += filter->linespace) {
235     for (x = 0; x <= width; x++) {
236       nsrc = src + (y * width) + x;
237
238       // Calc Y Value for curpix
239       R = ((*nsrc) & 0xff0000) >> (16 - 1);
240       G = ((*nsrc) & 0xff00) >> (8 - 2);
241       B = (*nsrc) & 0xff;
242
243       yval = y - ((short) (R + G + B) / filter->vscale);
244
245       if (yval > 0) {
246         dest[x + (yval * width)] = THE_COLOR;
247       }
248     }
249   }
250 }
251
252 static void
253 gst_revtv_set_property (GObject * object, guint prop_id, const GValue * value,
254     GParamSpec * pspec)
255 {
256   GstRevTV *filter;
257
258   g_return_if_fail (GST_IS_REVTV (object));
259
260   filter = GST_REVTV (object);
261
262   switch (prop_id) {
263     case ARG_DELAY:
264       filter->vgrabtime = g_value_get_int (value);
265       break;
266     case ARG_LINESPACE:
267       filter->linespace = g_value_get_int (value);
268       break;
269     case ARG_GAIN:
270       filter->vscale = g_value_get_int (value);
271       break;
272     default:
273       break;
274   }
275 }
276
277 static void
278 gst_revtv_get_property (GObject * object, guint prop_id, GValue * value,
279     GParamSpec * pspec)
280 {
281   GstRevTV *filter;
282
283   g_return_if_fail (GST_IS_REVTV (object));
284
285   filter = GST_REVTV (object);
286
287   switch (prop_id) {
288     case ARG_DELAY:
289       g_value_set_int (value, filter->vgrabtime);
290       break;
291     case ARG_LINESPACE:
292       g_value_set_int (value, filter->linespace);
293       break;
294     case ARG_GAIN:
295       g_value_set_int (value, filter->vscale);
296       break;
297     default:
298       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299       break;
300   }
301 }