controller: port to new controller location and api
[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 /**
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 ! videoconvert ! 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 #define THE_COLOR 0xffffffff
68
69 enum
70 {
71   PROP_0,
72   PROP_DELAY,
73   PROP_LINESPACE,
74   PROP_GAIN
75 };
76
77 #define gst_revtv_parent_class parent_class
78 G_DEFINE_TYPE (GstRevTV, gst_revtv, GST_TYPE_VIDEO_FILTER);
79
80 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
81 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx }")
82 #else
83 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
84 #endif
85
86 static GstStaticPadTemplate gst_revtv_src_template =
87 GST_STATIC_PAD_TEMPLATE ("src",
88     GST_PAD_SRC,
89     GST_PAD_ALWAYS,
90     GST_STATIC_CAPS (CAPS_STR)
91     );
92
93 static GstStaticPadTemplate gst_revtv_sink_template =
94 GST_STATIC_PAD_TEMPLATE ("sink",
95     GST_PAD_SINK,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS (CAPS_STR)
98     );
99
100 static gboolean
101 gst_revtv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
102     GstCaps * outcaps)
103 {
104   GstRevTV *filter = GST_REVTV (btrans);
105   GstVideoInfo info;
106
107   if (!gst_video_info_from_caps (&info, incaps))
108     goto invalid_caps;
109
110   filter->info = info;
111
112   return TRUE;
113
114   /* ERRORS */
115 invalid_caps:
116   {
117     GST_DEBUG_OBJECT (filter, "invalid caps received");
118     return FALSE;
119   }
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, sstride, dstride;
128   guint32 *nsrc;
129   gint y, x, R, G, B, yval;
130   gint linespace, vscale;
131   GstClockTime timestamp, stream_time;
132   GstVideoFrame in_frame, out_frame;
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 (GST_OBJECT (filter), stream_time);
143
144   if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
145     goto invalid_in;
146
147   if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
148     goto invalid_out;
149
150   src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
151   sstride = GST_VIDEO_FRAME_PLANE_STRIDE (&in_frame, 0);
152   dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
153   dstride = GST_VIDEO_FRAME_PLANE_STRIDE (&out_frame, 0);
154
155   width = GST_VIDEO_FRAME_WIDTH (&in_frame);
156   height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
157
158   /* Clear everything to black */
159   memset (dest, 0, dstride * height * sizeof (guint32));
160
161   GST_OBJECT_LOCK (filter);
162   linespace = filter->linespace;
163   vscale = filter->vscale;
164
165   /* draw the offset lines */
166   for (y = 0; y < height; y += linespace) {
167     for (x = 0; x <= width; x++) {
168       nsrc = src + (y * sstride) + x;
169
170       /* Calc Y Value for curpix */
171       R = ((*nsrc) & 0xff0000) >> (16 - 1);
172       G = ((*nsrc) & 0xff00) >> (8 - 2);
173       B = (*nsrc) & 0xff;
174
175       yval = y - ((short) (R + G + B) / vscale);
176
177       if (yval > 0) {
178         dest[x + (yval * dstride)] = THE_COLOR;
179       }
180     }
181   }
182   GST_OBJECT_UNLOCK (filter);
183
184   gst_video_frame_unmap (&in_frame);
185   gst_video_frame_unmap (&out_frame);
186
187   return GST_FLOW_OK;
188
189   /* ERRORS */
190 invalid_in:
191   {
192     GST_DEBUG_OBJECT (filter, "invalid input frame");
193     return GST_FLOW_ERROR;
194   }
195 invalid_out:
196   {
197     GST_DEBUG_OBJECT (filter, "invalid output frame");
198     gst_video_frame_unmap (&in_frame);
199     return GST_FLOW_ERROR;
200   }
201 }
202
203 static void
204 gst_revtv_set_property (GObject * object, guint prop_id, const GValue * value,
205     GParamSpec * pspec)
206 {
207   GstRevTV *filter = GST_REVTV (object);
208
209   GST_OBJECT_LOCK (filter);
210   switch (prop_id) {
211     case PROP_DELAY:
212       filter->vgrabtime = g_value_get_int (value);
213       break;
214     case PROP_LINESPACE:
215       filter->linespace = g_value_get_int (value);
216       break;
217     case PROP_GAIN:
218       filter->vscale = g_value_get_int (value);
219       break;
220     default:
221       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222       break;
223   }
224   GST_OBJECT_UNLOCK (filter);
225 }
226
227 static void
228 gst_revtv_get_property (GObject * object, guint prop_id, GValue * value,
229     GParamSpec * pspec)
230 {
231   GstRevTV *filter = GST_REVTV (object);
232
233   switch (prop_id) {
234     case PROP_DELAY:
235       g_value_set_int (value, filter->vgrabtime);
236       break;
237     case PROP_LINESPACE:
238       g_value_set_int (value, filter->linespace);
239       break;
240     case PROP_GAIN:
241       g_value_set_int (value, filter->vscale);
242       break;
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246   }
247 }
248
249 static void
250 gst_revtv_class_init (GstRevTVClass * klass)
251 {
252   GObjectClass *gobject_class = (GObjectClass *) klass;
253   GstElementClass *gstelement_class = (GstElementClass *) klass;
254   GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
255
256   gobject_class->set_property = gst_revtv_set_property;
257   gobject_class->get_property = gst_revtv_get_property;
258
259   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DELAY,
260       g_param_spec_int ("delay", "Delay", "Delay in frames between updates",
261           1, 100, 1,
262           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
263   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LINESPACE,
264       g_param_spec_int ("linespace", "Linespace", "Control line spacing", 1,
265           100, 6,
266           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
267   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GAIN,
268       g_param_spec_int ("gain", "Gain", "Control gain", 1, 200, 50,
269           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
270
271   gst_element_class_set_details_simple (gstelement_class, "RevTV effect",
272       "Filter/Effect/Video",
273       "A video waveform monitor for each line of video processed",
274       "Wim Taymans <wim.taymans@gmail.be>");
275
276   gst_element_class_add_pad_template (gstelement_class,
277       gst_static_pad_template_get (&gst_revtv_sink_template));
278   gst_element_class_add_pad_template (gstelement_class,
279       gst_static_pad_template_get (&gst_revtv_src_template));
280
281   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_revtv_set_caps);
282   trans_class->transform = GST_DEBUG_FUNCPTR (gst_revtv_transform);
283 }
284
285 static void
286 gst_revtv_init (GstRevTV * restv)
287 {
288   restv->vgrabtime = 1;
289   restv->vgrab = 0;
290   restv->linespace = 6;
291   restv->vscale = 50;
292 }