Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 /**
44  * SECTION:element-quarktv
45  *
46  * RevTV acts like a video waveform monitor for each line of video
47  * processed. This creates a pseudo 3D effect based on the brightness
48  * of the video along each line.
49  *
50  * <refsect2>
51  * <title>Example launch line</title>
52  * |[
53  * gst-launch -v videotestsrc ! revtv ! ffmpegcolorspace ! autovideosink
54  * ]| This pipeline shows the effect of revtv on a test stream.
55  * </refsect2>
56  */
57
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
61
62 #include <math.h>
63 #include <string.h>
64
65 #include "gstrev.h"
66
67 #include <gst/video/video.h>
68 #include <gst/controller/gstcontroller.h>
69
70 #define THE_COLOR 0xffffffff
71
72 enum
73 {
74   PROP_0,
75   PROP_DELAY,
76   PROP_LINESPACE,
77   PROP_GAIN
78 };
79
80 GST_BOILERPLATE (GstRevTV, gst_revtv, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
81
82 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
83 #define CAPS_STR GST_VIDEO_CAPS_BGRx ";" GST_VIDEO_CAPS_RGBx
84 #else
85 #define CAPS_STR GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_xRGB
86 #endif
87
88 static GstStaticPadTemplate gst_revtv_src_template =
89 GST_STATIC_PAD_TEMPLATE ("src",
90     GST_PAD_SRC,
91     GST_PAD_ALWAYS,
92     GST_STATIC_CAPS (CAPS_STR)
93     );
94
95 static GstStaticPadTemplate gst_revtv_sink_template =
96 GST_STATIC_PAD_TEMPLATE ("sink",
97     GST_PAD_SINK,
98     GST_PAD_ALWAYS,
99     GST_STATIC_CAPS (CAPS_STR)
100     );
101
102 static gboolean
103 gst_revtv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
104     GstCaps * outcaps)
105 {
106   GstRevTV *filter = GST_REVTV (btrans);
107   GstStructure *structure;
108   gboolean ret = FALSE;
109
110   structure = gst_caps_get_structure (incaps, 0);
111
112   GST_OBJECT_LOCK (filter);
113   if (gst_structure_get_int (structure, "width", &filter->width) &&
114       gst_structure_get_int (structure, "height", &filter->height)) {
115     ret = TRUE;
116   }
117   GST_OBJECT_UNLOCK (filter);
118
119   return ret;
120 }
121
122 static GstFlowReturn
123 gst_revtv_transform (GstBaseTransform * trans, GstBuffer * in, GstBuffer * out)
124 {
125   GstRevTV *filter = GST_REVTV (trans);
126   guint32 *src, *dest;
127   gint width, height;
128   guint32 *nsrc;
129   gint y, x, R, G, B, yval;
130   GstFlowReturn ret = GST_FLOW_OK;
131   gint linespace, vscale;
132   GstClockTime timestamp, stream_time;
133
134   timestamp = GST_BUFFER_TIMESTAMP (in);
135   stream_time =
136       gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
137
138   GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
139       GST_TIME_ARGS (timestamp));
140
141   if (GST_CLOCK_TIME_IS_VALID (stream_time))
142     gst_object_sync_values (G_OBJECT (filter), stream_time);
143
144   src = (guint32 *) GST_BUFFER_DATA (in);
145   dest = (guint32 *) GST_BUFFER_DATA (out);
146
147   GST_OBJECT_LOCK (filter);
148   width = filter->width;
149   height = filter->height;
150
151   /* Clear everything to black */
152   memset (dest, 0, width * height * sizeof (guint32));
153
154   linespace = filter->linespace;
155   vscale = filter->vscale;
156
157   /* draw the offset lines */
158   for (y = 0; y < height; y += linespace) {
159     for (x = 0; x <= width; x++) {
160       nsrc = src + (y * width) + x;
161
162       /* Calc Y Value for curpix */
163       R = ((*nsrc) & 0xff0000) >> (16 - 1);
164       G = ((*nsrc) & 0xff00) >> (8 - 2);
165       B = (*nsrc) & 0xff;
166
167       yval = y - ((short) (R + G + B) / vscale);
168
169       if (yval > 0) {
170         dest[x + (yval * width)] = THE_COLOR;
171       }
172     }
173   }
174   GST_OBJECT_UNLOCK (filter);
175
176   return ret;
177 }
178
179 static void
180 gst_revtv_set_property (GObject * object, guint prop_id, const GValue * value,
181     GParamSpec * pspec)
182 {
183   GstRevTV *filter = GST_REVTV (object);
184
185   GST_OBJECT_LOCK (filter);
186   switch (prop_id) {
187     case PROP_DELAY:
188       filter->vgrabtime = g_value_get_int (value);
189       break;
190     case PROP_LINESPACE:
191       filter->linespace = g_value_get_int (value);
192       break;
193     case PROP_GAIN:
194       filter->vscale = g_value_get_int (value);
195       break;
196     default:
197       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198       break;
199   }
200   GST_OBJECT_UNLOCK (filter);
201 }
202
203 static void
204 gst_revtv_get_property (GObject * object, guint prop_id, GValue * value,
205     GParamSpec * pspec)
206 {
207   GstRevTV *filter = GST_REVTV (object);
208
209   switch (prop_id) {
210     case PROP_DELAY:
211       g_value_set_int (value, filter->vgrabtime);
212       break;
213     case PROP_LINESPACE:
214       g_value_set_int (value, filter->linespace);
215       break;
216     case PROP_GAIN:
217       g_value_set_int (value, filter->vscale);
218       break;
219     default:
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221       break;
222   }
223 }
224
225 static void
226 gst_revtv_base_init (gpointer g_class)
227 {
228   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
229
230   gst_element_class_set_details_simple (element_class, "RevTV effect",
231       "Filter/Effect/Video",
232       "A video waveform monitor for each line of video processed",
233       "Wim Taymans <wim.taymans@chello.be>");
234
235   gst_element_class_add_static_pad_template (element_class,
236       &gst_revtv_sink_template);
237   gst_element_class_add_static_pad_template (element_class,
238       &gst_revtv_src_template);
239 }
240
241 static void
242 gst_revtv_class_init (GstRevTVClass * klass)
243 {
244   GObjectClass *gobject_class = (GObjectClass *) klass;
245   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
246
247   gobject_class->set_property = gst_revtv_set_property;
248   gobject_class->get_property = gst_revtv_get_property;
249
250   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DELAY,
251       g_param_spec_int ("delay", "Delay", "Delay in frames between updates",
252           1, 100, 1,
253           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
254   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LINESPACE,
255       g_param_spec_int ("linespace", "Linespace", "Control line spacing", 1,
256           100, 6,
257           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
258   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GAIN,
259       g_param_spec_int ("gain", "Gain", "Control gain", 1, 200, 50,
260           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
261
262   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_revtv_set_caps);
263   trans_class->transform = GST_DEBUG_FUNCPTR (gst_revtv_transform);
264 }
265
266 static void
267 gst_revtv_init (GstRevTV * restv, GstRevTVClass * klass)
268 {
269   restv->vgrabtime = 1;
270   restv->vgrab = 0;
271   restv->linespace = 6;
272   restv->vscale = 50;
273 }