1 /* GStreamer
2 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
3 *
4 * EffecTV - Realtime Digital Video Effector
5 * Copyright (C) 2001-2006 FUKUCHI Kentaro
6 *
7 * OpTV - Optical art meets real-time video effect.
8 * Copyright (C) 2004-2005 FUKUCHI Kentaro
9 *
10 * EffecTV is free software. This library is free software;
11 * you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
25 */
26
27 /**
28 * SECTION:element-optv
29 *
30 * Traditional black-white optical animation is now resurrected as a
31 * real-time video effect. Input images are binarized and combined with
32 * various optical pattern.
33 *
34 * <refsect2>
35 * <title>Example launch line</title>
36 * |[
37 * gst-launch-1.0 -v videotestsrc ! optv ! videoconvert ! autovideosink
38 * ]| This pipeline shows the effect of optv on a test stream.
39 * </refsect2>
40 */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <math.h>
47 #include <string.h>
48
49 #include "gstop.h"
50 #include "gsteffectv.h"
51
52 #include <gst/video/video.h>
53
54 enum
55 {
56 OP_SPIRAL1 = 0,
57 OP_SPIRAL2,
58 OP_PARABOLA,
59 OP_HSTRIPE
60 };
61
62 #define GST_TYPE_OPTV_MODE (gst_optv_mode_get_type())
63 static GType
gst_optv_mode_get_type(void)64 gst_optv_mode_get_type (void)
65 {
66 static GType type = 0;
67
68 static const GEnumValue enumvalue[] = {
69 {OP_SPIRAL1, "Maelstrom", "maelstrom"},
70 {OP_SPIRAL2, "Radiation", "radiation"},
71 {OP_PARABOLA, "Horizontal Stripes",
72 "horizontal-stripes"},
73 {OP_HSTRIPE, "Vertical Stripes", "vertical-stripes"},
74 {0, NULL, NULL},
75 };
76
77 if (!type) {
78 type = g_enum_register_static ("GstOpTVMode", enumvalue);
79 }
80 return type;
81 }
82
83 #define DEFAULT_MODE OP_SPIRAL1
84 #define DEFAULT_SPEED 16
85 #define DEFAULT_THRESHOLD 60
86
87 enum
88 {
89 PROP_0,
90 PROP_MODE,
91 PROP_SPEED,
92 PROP_THRESHOLD
93 };
94
95 static guint32 palette[256];
96
97 #define gst_optv_parent_class parent_class
98 G_DEFINE_TYPE (GstOpTV, gst_optv, GST_TYPE_VIDEO_FILTER);
99
100 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
101 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx }")
102 #else
103 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
104 #endif
105
106 static GstStaticPadTemplate gst_optv_src_template =
107 GST_STATIC_PAD_TEMPLATE ("src",
108 GST_PAD_SRC,
109 GST_PAD_ALWAYS,
110 GST_STATIC_CAPS (CAPS_STR)
111 );
112
113 static GstStaticPadTemplate gst_optv_sink_template =
114 GST_STATIC_PAD_TEMPLATE ("sink",
115 GST_PAD_SINK,
116 GST_PAD_ALWAYS,
117 GST_STATIC_CAPS (CAPS_STR)
118 );
119
120 static void
initPalette(void)121 initPalette (void)
122 {
123 gint i;
124 guint8 v;
125
126 for (i = 0; i < 112; i++) {
127 palette[i] = 0;
128 palette[i + 128] = 0xffffff;
129 }
130 for (i = 0; i < 16; i++) {
131 v = 16 * (i + 1) - 1;
132 palette[i + 112] = (v << 16) | (v << 8) | v;
133 v = 255 - v;
134 palette[i + 240] = (v << 16) | (v << 8) | v;
135 }
136 }
137
138 static void
setOpmap(gint8 * opmap[4],gint width,gint height)139 setOpmap (gint8 * opmap[4], gint width, gint height)
140 {
141 gint i, j, x, y;
142 #ifndef PS2
143 gdouble xx, yy, r, at, rr;
144 #else
145 gfloat xx, yy, r, at, rr;
146 #endif
147 gint sci;
148
149 sci = 640 / width;
150 i = 0;
151 for (y = 0; y < height; y++) {
152 yy = (gdouble) (y - height / 2) / width;
153 for (x = 0; x < width; x++) {
154 xx = (gdouble) x / width - 0.5;
155 #ifndef PS2
156 r = sqrt (xx * xx + yy * yy);
157 at = atan2 (xx, yy);
158 #else
159 r = sqrtf (xx * xx + yy * yy);
160 at = atan2f (xx, yy);
161 #endif
162
163 opmap[OP_SPIRAL1][i] = ((guint)
164 ((at / G_PI * 256) + (r * 4000))) & 255;
165
166 j = r * 300 / 32;
167 rr = r * 300 - j * 32;
168 j *= 64;
169 j += (rr > 28) ? (rr - 28) * 16 : 0;
170 opmap[OP_SPIRAL2][i] = ((guint)
171 ((at / G_PI * 4096) + (r * 1600) - j)) & 255;
172
173 opmap[OP_PARABOLA][i] =
174 ((guint) (yy / (xx * xx * 0.3 + 0.1) * 400)) & 255;
175 opmap[OP_HSTRIPE][i] = x * 8 * sci;
176 i++;
177 }
178 }
179 }
180
181 /* Taken from effectv/image.c */
182 /* Y value filters */
183 static void
image_y_over(guint32 * src,guint8 * diff,gint y_threshold,gint video_area)184 image_y_over (guint32 * src, guint8 * diff, gint y_threshold, gint video_area)
185 {
186 gint i;
187 gint R, G, B, v;
188 guint8 *p = diff;
189
190 for (i = video_area; i > 0; i--) {
191 R = ((*src) & 0xff0000) >> (16 - 1);
192 G = ((*src) & 0xff00) >> (8 - 2);
193 B = (*src) & 0xff;
194 v = y_threshold * 7 - (R + G + B);
195 *p = (guint8) (v >> 24);
196 src++;
197 p++;
198 }
199 }
200
201 static GstFlowReturn
gst_optv_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)202 gst_optv_transform_frame (GstVideoFilter * vfilter, GstVideoFrame * in_frame,
203 GstVideoFrame * out_frame)
204 {
205 GstOpTV *filter = GST_OPTV (vfilter);
206 guint32 *src, *dest;
207 gint8 *p;
208 guint8 *diff;
209 gint x, y, width, height;
210 GstClockTime timestamp, stream_time;
211 guint8 phase;
212
213 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
214 stream_time =
215 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (vfilter)->segment,
216 GST_FORMAT_TIME, timestamp);
217
218 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
219 GST_TIME_ARGS (timestamp));
220
221 if (GST_CLOCK_TIME_IS_VALID (stream_time))
222 gst_object_sync_values (GST_OBJECT (filter), stream_time);
223
224 if (G_UNLIKELY (filter->opmap[0] == NULL))
225 return GST_FLOW_NOT_NEGOTIATED;
226
227 src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
228 dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
229
230 width = GST_VIDEO_FRAME_WIDTH (in_frame);
231 height = GST_VIDEO_FRAME_HEIGHT (in_frame);
232
233 GST_OBJECT_LOCK (filter);
234 switch (filter->mode) {
235 default:
236 case 0:
237 p = filter->opmap[OP_SPIRAL1];
238 break;
239 case 1:
240 p = filter->opmap[OP_SPIRAL2];
241 break;
242 case 2:
243 p = filter->opmap[OP_PARABOLA];
244 break;
245 case 3:
246 p = filter->opmap[OP_HSTRIPE];
247 break;
248 }
249
250 filter->phase -= filter->speed;
251
252 diff = filter->diff;
253 image_y_over (src, diff, filter->threshold, width * height);
254 phase = filter->phase;
255
256 for (y = 0; y < height; y++) {
257 for (x = 0; x < width; x++) {
258 *dest++ = palette[(((guint8) (*p + phase)) ^ *diff++) & 255];
259 p++;
260 }
261 }
262 GST_OBJECT_UNLOCK (filter);
263
264 return GST_FLOW_OK;
265 }
266
267 static gboolean
gst_optv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)268 gst_optv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
269 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
270 {
271 GstOpTV *filter = GST_OPTV (vfilter);
272 gint i, width, height;
273
274 width = GST_VIDEO_INFO_WIDTH (in_info);
275 height = GST_VIDEO_INFO_HEIGHT (in_info);
276
277 for (i = 0; i < 4; i++) {
278 g_free (filter->opmap[i]);
279 filter->opmap[i] = g_new (gint8, width * height);
280 }
281 setOpmap (filter->opmap, width, height);
282
283 g_free (filter->diff);
284 filter->diff = g_new (guint8, width * height);
285
286 return TRUE;
287 }
288
289 static gboolean
gst_optv_start(GstBaseTransform * trans)290 gst_optv_start (GstBaseTransform * trans)
291 {
292 GstOpTV *filter = GST_OPTV (trans);
293
294 filter->phase = 0;
295
296 return TRUE;
297 }
298
299 static void
gst_optv_finalize(GObject * object)300 gst_optv_finalize (GObject * object)
301 {
302 GstOpTV *filter = GST_OPTV (object);
303
304 if (filter->opmap[0]) {
305 gint i;
306
307 for (i = 0; i < 4; i++) {
308 g_free (filter->opmap[i]);
309 filter->opmap[i] = NULL;
310 }
311 }
312
313 g_free (filter->diff);
314 filter->diff = NULL;
315
316 G_OBJECT_CLASS (parent_class)->finalize (object);
317 }
318
319 static void
gst_optv_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)320 gst_optv_set_property (GObject * object, guint prop_id, const GValue * value,
321 GParamSpec * pspec)
322 {
323 GstOpTV *filter = GST_OPTV (object);
324
325 GST_OBJECT_LOCK (filter);
326 switch (prop_id) {
327 case PROP_MODE:
328 filter->mode = g_value_get_enum (value);
329 break;
330 case PROP_SPEED:
331 filter->speed = g_value_get_int (value);
332 break;
333 case PROP_THRESHOLD:
334 filter->threshold = g_value_get_uint (value);
335 break;
336 default:
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338 break;
339 }
340 GST_OBJECT_UNLOCK (filter);
341 }
342
343 static void
gst_optv_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)344 gst_optv_get_property (GObject * object, guint prop_id, GValue * value,
345 GParamSpec * pspec)
346 {
347 GstOpTV *filter = GST_OPTV (object);
348
349 switch (prop_id) {
350 case PROP_MODE:
351 g_value_set_enum (value, filter->mode);
352 break;
353 case PROP_SPEED:
354 g_value_set_int (value, filter->speed);
355 break;
356 case PROP_THRESHOLD:
357 g_value_set_uint (value, filter->threshold);
358 break;
359 default:
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 break;
362 }
363 }
364
365 static void
gst_optv_class_init(GstOpTVClass * klass)366 gst_optv_class_init (GstOpTVClass * klass)
367 {
368 GObjectClass *gobject_class = (GObjectClass *) klass;
369 GstElementClass *gstelement_class = (GstElementClass *) klass;
370 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
371 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
372
373 gobject_class->set_property = gst_optv_set_property;
374 gobject_class->get_property = gst_optv_get_property;
375
376 gobject_class->finalize = gst_optv_finalize;
377
378 g_object_class_install_property (gobject_class, PROP_MODE,
379 g_param_spec_enum ("mode", "Mode",
380 "Mode", GST_TYPE_OPTV_MODE, DEFAULT_MODE,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
382
383 g_object_class_install_property (gobject_class, PROP_SPEED,
384 g_param_spec_int ("speed", "Speed",
385 "Effect speed", G_MININT, G_MAXINT, DEFAULT_SPEED,
386 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
387
388 g_object_class_install_property (gobject_class, PROP_THRESHOLD,
389 g_param_spec_uint ("threshold", "Threshold",
390 "Luma threshold", 0, G_MAXINT, DEFAULT_THRESHOLD,
391 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392
393 gst_element_class_set_static_metadata (gstelement_class, "OpTV effect",
394 "Filter/Effect/Video",
395 "Optical art meets real-time video effect",
396 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
397 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
398
399 gst_element_class_add_static_pad_template (gstelement_class,
400 &gst_optv_sink_template);
401 gst_element_class_add_static_pad_template (gstelement_class,
402 &gst_optv_src_template);
403
404 trans_class->start = GST_DEBUG_FUNCPTR (gst_optv_start);
405
406 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_optv_set_info);
407 vfilter_class->transform_frame = GST_DEBUG_FUNCPTR (gst_optv_transform_frame);
408
409 initPalette ();
410 }
411
412 static void
gst_optv_init(GstOpTV * filter)413 gst_optv_init (GstOpTV * filter)
414 {
415 filter->speed = DEFAULT_SPEED;
416 filter->mode = DEFAULT_MODE;
417 filter->threshold = DEFAULT_THRESHOLD;
418 }
419