2 * Copyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * EffecTV - Realtime Digital Video Effector
5 * Copyright (C) 2001-2006 FUKUCHI Kentaro
7 * RippleTV - Water ripple effect.
8 * Copyright (C) 2001-2002 FUKUCHI Kentaro
10 * This combines the RippleTV and BaltanTV effects, which are
11 * very similar. BaltanTV is used if the feedback property is set
12 * to TRUE, otherwise RippleTV is used.
14 * EffecTV is free software. This library is free software;
15 * you can redistribute it and/or
16 * modify it under the terms of the GNU Library General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Library General Public License for more details.
25 * You should have received a copy of the GNU Library General Public
26 * License along with this library; if not, write to the
27 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
32 * SECTION:element-rippletv
34 * RippleTV does ripple mark effect on the video input. The ripple is caused
35 * by motion or random rain drops.
38 * <title>Example launch line</title>
40 * gst-launch -v videotestsrc ! rippletv ! videoconvert ! autovideosink
41 * ]| This pipeline shows the effect of rippletv on a test stream.
52 #include "gstripple.h"
53 #include "gsteffectv.h"
55 #define DEFAULT_MODE 0
64 static gint sqrtable[256];
66 #define GST_TYPE_RIPPLETV_MODE (gst_rippletv_mode_get_type())
68 gst_rippletv_mode_get_type (void)
70 static GType type = 0;
72 static const GEnumValue enumvalue[] = {
73 {0, "Motion Detection", "motion-detection"},
79 type = g_enum_register_static ("GstRippleTVMode", enumvalue);
84 #define gst_rippletv_parent_class parent_class
85 G_DEFINE_TYPE (GstRippleTV, gst_rippletv, GST_TYPE_VIDEO_FILTER);
87 static GstStaticPadTemplate gst_rippletv_src_template =
88 GST_STATIC_PAD_TEMPLATE ("src",
91 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx, xBGR, xRGB }"))
94 static GstStaticPadTemplate gst_rippletv_sink_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
98 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ BGRx, RGBx, xBGR, xRGB }"))
101 static const gint point = 16;
102 static const gint impact = 2;
103 static const gint decay = 8;
104 static const gint loopnum = 2;
111 for (i = 0; i < 128; i++) {
114 for (i = 1; i <= 128; i++) {
115 sqrtable[256 - i] = -i * i;
120 image_bgset_y (guint32 * src, gint16 * background, gint video_area)
129 for (i = 0; i < video_area; i++) {
130 R = ((*p) & 0xff0000) >> (16 - 1);
131 G = ((*p) & 0xff00) >> (8 - 2);
133 *q = (gint16) (R + G + B);
140 setBackground (GstRippleTV * filter, guint32 * src)
142 image_bgset_y (src, filter->background,
143 GST_VIDEO_INFO_WIDTH (&filter->info) *
144 GST_VIDEO_INFO_HEIGHT (&filter->info));
145 filter->bg_is_set = TRUE;
151 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
164 for (i = 0; i < video_area; i++) {
165 R = ((*p) & 0xff0000) >> (16 - 1);
166 G = ((*p) & 0xff00) >> (8 - 2);
168 v = (R + G + B) - (gint) (*q);
169 *q = (gint16) (R + G + B);
170 *r = ((v + 70 * 7) >> 24) | ((70 * 7 - v) >> 24);
179 motiondetect (GstRippleTV * filter, guint32 * src)
181 guint8 *diff = filter->diff;
186 width = GST_VIDEO_INFO_WIDTH (&filter->info);
187 height = GST_VIDEO_INFO_HEIGHT (&filter->info);
189 if (!filter->bg_is_set)
190 setBackground (filter, src);
192 image_bgsubtract_update_y (src, filter->background, filter->diff,
194 p = filter->map1 + filter->map_w + 1;
195 q = filter->map2 + filter->map_w + 1;
198 for (y = filter->map_h - 2; y > 0; y--) {
199 for (x = filter->map_w - 2; x > 0; x--) {
200 h = (gint) * diff + (gint) * (diff + 1) + (gint) * (diff + width) +
201 (gint) * (diff + width + 1);
203 *p = h << (point + impact - 8);
217 drop (gint power, gint * map1, gint * map2, gint map_w, gint map_h)
222 x = fastrand () % (map_w - 4) + 2;
223 y = fastrand () % (map_h - 4) + 2;
224 p = map1 + y * map_w + x;
225 q = map2 + y * map_w + x;
228 *(p - map_w) = *(p - 1) = *(p + 1) = *(p + map_w) = power / 2;
229 *(p - map_w - 1) = *(p - map_w + 1) = *(p + map_w - 1) = *(p + map_w + 1) =
231 *(q - map_w) = *(q - 1) = *(q + 1) = *(q + map_w) = power / 2;
232 *(q - map_w - 1) = *(q - map_w + 1) = *(q + map_w - 1) = *(p + map_w + 1) =
237 raindrop (GstRippleTV * filter)
241 if (filter->period == 0) {
242 switch (filter->rain_stat) {
244 filter->period = (fastrand () >> 23) + 100;
245 filter->drop_prob = 0;
246 filter->drop_prob_increment = 0x00ffffff / filter->period;
247 filter->drop_power = (-(fastrand () >> 28) - 2) << point;
248 filter->drops_per_frame_max = 2 << (fastrand () >> 30); // 2,4,8 or 16
249 filter->rain_stat = 1;
252 filter->drop_prob = 0x00ffffff;
253 filter->drops_per_frame = 1;
254 filter->drop_prob_increment = 1;
255 filter->period = (filter->drops_per_frame_max - 1) * 16;
256 filter->rain_stat = 2;
259 filter->period = (fastrand () >> 22) + 1000;
260 filter->drop_prob_increment = 0;
261 filter->rain_stat = 3;
264 filter->period = (filter->drops_per_frame_max - 1) * 16;
265 filter->drop_prob_increment = -1;
266 filter->rain_stat = 4;
269 filter->period = (fastrand () >> 24) + 60;
270 filter->drop_prob_increment = -(filter->drop_prob / filter->period);
271 filter->rain_stat = 5;
275 filter->period = (fastrand () >> 23) + 500;
276 filter->drop_prob = 0;
277 filter->rain_stat = 0;
281 switch (filter->rain_stat) {
287 if ((fastrand () >> 8) < filter->drop_prob) {
288 drop (filter->drop_power, filter->map1, filter->map2, filter->map_w,
291 filter->drop_prob += filter->drop_prob_increment;
296 for (i = filter->drops_per_frame / 16; i > 0; i--) {
297 drop (filter->drop_power, filter->map1, filter->map2, filter->map_w,
300 filter->drops_per_frame += filter->drop_prob_increment;
307 gst_rippletv_transform (GstBaseTransform * trans, GstBuffer * in,
310 GstRippleTV *filter = GST_RIPPLETV (trans);
312 GstVideoFrame in_frame, out_frame;
316 gint m_w, m_h, v_w, v_h;
319 GstClockTime timestamp, stream_time;
321 timestamp = GST_BUFFER_TIMESTAMP (in);
323 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
325 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
326 GST_TIME_ARGS (timestamp));
328 if (GST_CLOCK_TIME_IS_VALID (stream_time))
329 gst_object_sync_values (GST_OBJECT (filter), stream_time);
331 if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
334 if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
337 src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
338 dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
340 GST_OBJECT_LOCK (filter);
341 /* impact from the motion or rain drop */
345 motiondetect (filter, src);
349 v_w = GST_VIDEO_FRAME_WIDTH (&in_frame);
350 v_h = GST_VIDEO_FRAME_HEIGHT (&in_frame);
352 /* simulate surface wave */
354 /* This function is called only 30 times per second. To increase a speed
355 * of wave, iterates this loop several times. */
356 for (i = loopnum; i > 0; i--) {
357 /* wave simulation */
358 p = filter->map1 + m_w + 1;
359 q = filter->map2 + m_w + 1;
360 r = filter->map3 + m_w + 1;
361 for (y = m_h - 2; y > 0; y--) {
362 for (x = m_w - 2; x > 0; x--) {
363 h = *(p - m_w - 1) + *(p - m_w + 1) + *(p + m_w - 1) + *(p + m_w + 1)
364 + *(p - m_w) + *(p - 1) + *(p + 1) + *(p + m_w) - (*p) * 9;
367 v += h - (v >> decay);
378 /* low pass filter */
379 p = filter->map3 + m_w + 1;
380 q = filter->map2 + m_w + 1;
381 for (y = m_h - 2; y > 0; y--) {
382 for (x = m_w - 2; x > 0; x--) {
383 h = *(p - m_w) + *(p - 1) + *(p + 1) + *(p + m_w) + (*p) * 60;
393 filter->map1 = filter->map2;
399 for (y = m_h - 1; y > 0; y--) {
400 for (x = m_w - 1; x > 0; x--) {
401 /* difference of the height between two voxel. They are twiced to
402 * emphasise the wave. */
403 vp[0] = sqrtable[((p[0] - p[1]) >> (point - 1)) & 0xff];
404 vp[1] = sqrtable[((p[0] - p[m_w]) >> (point - 1)) & 0xff];
414 /* draw refracted image. The vector table is stretched. */
415 for (y = 0; y < v_h; y += 2) {
416 for (x = 0; x < v_w; x += 2) {
421 dx = CLAMP (dx, 0, (v_w - 1));
422 dy = CLAMP (dy, 0, (v_h - 1));
423 dest[0] = src[dy * v_w + dx];
427 dx = x + 1 + (h + (gint) vp[2]) / 2;
428 dx = CLAMP (dx, 0, (v_w - 1));
429 dest[1] = src[dy * v_w + dx];
431 dy = y + 1 + (v + (gint) vp[m_w * 2 + 1]) / 2;
432 dy = CLAMP (dy, 0, (v_h - 1));
433 dest[v_w] = src[dy * v_w + o_dx];
435 dest[v_w + 1] = src[dy * v_w + dx];
442 GST_OBJECT_UNLOCK (filter);
444 gst_video_frame_unmap (&in_frame);
445 gst_video_frame_unmap (&out_frame);
452 GST_DEBUG_OBJECT (filter, "invalid input frame");
453 return GST_FLOW_ERROR;
457 GST_DEBUG_OBJECT (filter, "invalid output frame");
458 gst_video_frame_unmap (&in_frame);
459 return GST_FLOW_ERROR;
465 gst_rippletv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
468 GstRippleTV *filter = GST_RIPPLETV (btrans);
472 if (!gst_video_info_from_caps (&info, incaps))
477 width = GST_VIDEO_INFO_WIDTH (&info);
478 height = GST_VIDEO_INFO_HEIGHT (&info);
480 GST_OBJECT_LOCK (filter);
481 filter->map_h = height / 2 + 1;
482 filter->map_w = width / 2 + 1;
484 /* we over allocate the buffers, as the render code does not handle clipping
487 g_free (filter->map);
488 filter->map = g_new0 (gint, (1 + filter->map_h) * filter->map_w * 3);
490 filter->map1 = filter->map;
491 filter->map2 = filter->map + filter->map_w * filter->map_h;
492 filter->map3 = filter->map + filter->map_w * filter->map_h * 2;
495 g_free (filter->vtable);
496 filter->vtable = g_new0 (gint8, (1 + filter->map_h) * filter->map_w * 2);
498 if (filter->background)
499 g_free (filter->background);
500 filter->background = g_new0 (gint16, width * (height + 1));
503 g_free (filter->diff);
504 filter->diff = g_new0 (guint8, width * (height + 1));
505 GST_OBJECT_UNLOCK (filter);
512 GST_DEBUG_OBJECT (filter, "invalid caps received");
518 gst_rippletv_start (GstBaseTransform * trans)
520 GstRippleTV *filter = GST_RIPPLETV (trans);
522 filter->bg_is_set = FALSE;
525 filter->rain_stat = 0;
526 filter->drop_prob = 0;
527 filter->drop_prob_increment = 0;
528 filter->drops_per_frame_max = 0;
529 filter->drops_per_frame = 0;
530 filter->drop_power = 0;
536 gst_rippletv_finalize (GObject * object)
538 GstRippleTV *filter = GST_RIPPLETV (object);
541 g_free (filter->map);
545 g_free (filter->vtable);
546 filter->vtable = NULL;
548 if (filter->background)
549 g_free (filter->background);
550 filter->background = NULL;
553 g_free (filter->diff);
556 G_OBJECT_CLASS (parent_class)->finalize (object);
560 gst_rippletv_set_property (GObject * object, guint prop_id,
561 const GValue * value, GParamSpec * pspec)
563 GstRippleTV *filter = GST_RIPPLETV (object);
565 GST_OBJECT_LOCK (filter);
568 memset (filter->map, 0,
569 filter->map_h * filter->map_w * 2 * sizeof (gint));
573 filter->mode = g_value_get_enum (value);
576 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
579 GST_OBJECT_UNLOCK (filter);
583 gst_rippletv_get_property (GObject * object, guint prop_id, GValue * value,
586 GstRippleTV *filter = GST_RIPPLETV (object);
590 g_value_set_enum (value, filter->mode);
593 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
599 gst_rippletv_class_init (GstRippleTVClass * klass)
601 GObjectClass *gobject_class = (GObjectClass *) klass;
602 GstElementClass *gstelement_class = (GstElementClass *) klass;
603 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
605 gobject_class->set_property = gst_rippletv_set_property;
606 gobject_class->get_property = gst_rippletv_get_property;
608 gobject_class->finalize = gst_rippletv_finalize;
610 g_object_class_install_property (gobject_class, PROP_RESET,
611 g_param_spec_boolean ("reset", "Reset",
612 "Reset all current ripples", FALSE,
613 G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
615 g_object_class_install_property (gobject_class, PROP_MODE,
616 g_param_spec_enum ("mode", "Mode",
617 "Mode", GST_TYPE_RIPPLETV_MODE, DEFAULT_MODE,
618 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
620 gst_element_class_set_details_simple (gstelement_class, "RippleTV effect",
621 "Filter/Effect/Video",
622 "RippleTV does ripple mark effect on the video input",
623 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
624 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
626 gst_element_class_add_pad_template (gstelement_class,
627 gst_static_pad_template_get (&gst_rippletv_sink_template));
628 gst_element_class_add_pad_template (gstelement_class,
629 gst_static_pad_template_get (&gst_rippletv_src_template));
631 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_rippletv_set_caps);
632 trans_class->transform = GST_DEBUG_FUNCPTR (gst_rippletv_transform);
633 trans_class->start = GST_DEBUG_FUNCPTR (gst_rippletv_start);
639 gst_rippletv_init (GstRippleTV * filter)
641 filter->mode = DEFAULT_MODE;
643 /* FIXME: remove this when memory corruption after resizes are fixed */
644 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
645 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));