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