2 * Cradioacyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * EffecTV - Realtime Digital Video Effector
5 * Cradioacyright (C) 2001-2006 FUKUCHI Kentaro
7 * RadioacTV - motion-enlightment effect.
8 * Cradioacyright (C) 2001-2002 FUKUCHI Kentaro
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 radioaction) any later version.
16 * This library is distributed in the hradioace 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.
21 * You should have received a cradioacy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
28 * SECTION:element-radioactv
30 * RadioacTV does *NOT* detect a radioactivity. It detects a difference
31 * from previous frame and blurs it.
33 * RadioacTV has 4 mode, normal, strobe1, strobe2 and trigger.
34 * In trigger mode, effect appears only when the trigger property is %TRUE.
36 * strobe1 and strobe2 mode drops some frames. strobe1 mode uses the difference between
37 * current frame and previous frame dropped, while strobe2 mode uses the difference from
38 * previous frame displayed. The effect of strobe2 is stronger than strobe1.
41 * <title>Example launch line</title>
43 * gst-launch -v videotestsrc ! radioactv ! videoconvert ! autovideosink
44 * ]| This pipeline shows the effect of radioactv on a test stream.
55 #include "gstradioac.h"
56 #include "gsteffectv.h"
58 #include <gst/controller/gstcontroller.h>
76 #define GST_TYPE_RADIOACTV_MODE (gst_radioactv_mode_get_type())
78 gst_radioactv_mode_get_type (void)
80 static GType type = 0;
82 static const GEnumValue enumvalue[] = {
83 {RADIOAC_NORMAL, "Normal", "normal"},
84 {RADIOAC_STROBE, "Strobe 1", "strobe1"},
85 {RADIOAC_STROBE2, "Strobe 2", "strobe2"},
86 {RADIOAC_TRIGGER, "Trigger", "trigger"},
91 type = g_enum_register_static ("GstRadioacTVMode", enumvalue);
96 #define GST_TYPE_RADIOACTV_COLOR (gst_radioactv_color_get_type())
98 gst_radioactv_color_get_type (void)
100 static GType type = 0;
102 static const GEnumValue enumvalue[] = {
103 {COLOR_RED, "Red", "red"},
104 {COLOR_GREEN, "Green", "green"},
105 {COLOR_BLUE, "Blue", "blue"},
106 {COLOR_WHITE, "White", "white"},
111 type = g_enum_register_static ("GstRadioacTVColor", enumvalue);
116 #define DEFAULT_MODE RADIOAC_NORMAL
117 #define DEFAULT_COLOR COLOR_WHITE
118 #define DEFAULT_INTERVAL 3
119 #define DEFAULT_TRIGGER FALSE
132 #define MAGIC_THRESHOLD 40
135 static guint32 palettes[COLORS * PATTERN];
137 #define gst_radioactv_parent_class parent_class
138 G_DEFINE_TYPE (GstRadioacTV, gst_radioactv, GST_TYPE_VIDEO_FILTER);
140 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
141 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("RGBx")
143 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("xBGR")
146 static GstStaticPadTemplate gst_radioactv_src_template =
147 GST_STATIC_PAD_TEMPLATE ("src",
150 GST_STATIC_CAPS (CAPS_STR)
153 static GstStaticPadTemplate gst_radioactv_sink_template =
154 GST_STATIC_PAD_TEMPLATE ("sink",
157 GST_STATIC_CAPS (CAPS_STR)
165 #define DELTA (255/(COLORS/2-1))
167 for (i = 0; i < COLORS / 2; i++) {
168 palettes[i] = i * DELTA;
169 palettes[COLORS + i] = (i * DELTA) << 8;
170 palettes[COLORS * 2 + i] = (i * DELTA) << 16;
172 for (i = 0; i < COLORS / 2; i++) {
173 palettes[+i + COLORS / 2] = 255 | (i * DELTA) << 16 | (i * DELTA) << 8;
174 palettes[COLORS + i + COLORS / 2] =
175 (255 << 8) | (i * DELTA) << 16 | i * DELTA;
176 palettes[COLORS * 2 + i + COLORS / 2] =
177 (255 << 16) | (i * DELTA) << 8 | i * DELTA;
179 for (i = 0; i < COLORS; i++) {
180 palettes[COLORS * 3 + i] = (255 * i / COLORS) * 0x10101;
182 for (i = 0; i < COLORS * PATTERN; i++) {
183 palettes[i] = palettes[i] & 0xfefeff;
188 #define VIDEO_HWIDTH (filter->buf_width/2)
189 #define VIDEO_HHEIGHT (filter->buf_height/2)
191 /* this table assumes that video_width is times of 32 */
193 setTable (GstRadioacTV * filter)
196 gint x, y, tx, ty, xx;
199 prevptr = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
200 for (xx = 0; xx < (filter->buf_width_blocks); xx++) {
202 for (x = 0; x < 32; x++) {
203 ptr = (gint) (0.5 + RATIO * (xx * 32 + x - VIDEO_HWIDTH) + VIDEO_HWIDTH);
209 filter->blurzoomx[xx] = bits;
212 ty = (gint) (0.5 + RATIO * (-VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
213 tx = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
214 xx = (gint) (0.5 + RATIO * (filter->buf_width - 1 - VIDEO_HWIDTH) +
216 filter->blurzoomy[0] = ty * filter->buf_width + tx;
217 prevptr = ty * filter->buf_width + xx;
218 for (y = 1; y < filter->buf_height; y++) {
219 ty = (gint) (0.5 + RATIO * (y - VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
220 filter->blurzoomy[y] = ty * filter->buf_width + tx - prevptr;
221 prevptr = ty * filter->buf_width + xx;
229 blur (GstRadioacTV * filter)
236 width = filter->buf_width;
237 p = filter->blurzoombuf + GST_VIDEO_INFO_WIDTH (&filter->info) + 1;
238 q = p + filter->buf_area;
240 for (y = filter->buf_height - 2; y > 0; y--) {
241 for (x = width - 2; x > 0; x--) {
242 v = (*(p - width) + *(p - 1) + *(p + 1) + *(p + width)) / 4 - 1;
255 zoom (GstRadioacTV * filter)
262 p = filter->blurzoombuf + filter->buf_area;
263 q = filter->blurzoombuf;
264 height = filter->buf_height;
265 blocks = filter->buf_width_blocks;
267 for (y = 0; y < height; y++) {
268 p += filter->blurzoomy[y];
269 for (b = 0; b < blocks; b++) {
270 dx = filter->blurzoomx[b];
271 for (x = 0; x < 32; x++) {
281 blurzoomcore (GstRadioacTV * filter)
287 /* Background image is refreshed every frame */
289 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
290 gint video_area, gint y_threshold)
302 for (i = 0; i < video_area; i++) {
303 R = ((*p) & 0xff0000) >> (16 - 1);
304 G = ((*p) & 0xff00) >> (8 - 2);
306 v = (R + G + B) - (gint) (*q);
307 *q = (gint16) (R + G + B);
308 *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24);
317 gst_radioactv_transform (GstBaseTransform * trans, GstBuffer * in,
320 GstRadioacTV *filter = GST_RADIOACTV (trans);
322 GstVideoFrame in_frame, out_frame;
323 GstClockTime timestamp, stream_time;
324 gint x, y, width, height;
329 timestamp = GST_BUFFER_TIMESTAMP (in);
331 gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);
333 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
334 GST_TIME_ARGS (timestamp));
336 if (GST_CLOCK_TIME_IS_VALID (stream_time))
337 gst_object_sync_values (G_OBJECT (filter), stream_time);
339 if (!gst_video_frame_map (&in_frame, &filter->info, in, GST_MAP_READ))
342 if (!gst_video_frame_map (&out_frame, &filter->info, out, GST_MAP_WRITE))
345 src = GST_VIDEO_FRAME_PLANE_DATA (&in_frame, 0);
346 dest = GST_VIDEO_FRAME_PLANE_DATA (&out_frame, 0);
348 width = GST_VIDEO_FRAME_WIDTH (&in_frame);
349 height = GST_VIDEO_FRAME_HEIGHT (&in_frame);
351 GST_OBJECT_LOCK (filter);
352 palette = &palettes[COLORS * filter->color];
355 if (filter->mode == 3 && filter->trigger)
356 filter->snaptime = 0;
357 else if (filter->mode == 3 && !filter->trigger)
358 filter->snaptime = 1;
360 if (filter->mode != 2 || filter->snaptime <= 0) {
361 image_bgsubtract_update_y (src, filter->background, diff,
362 width * height, MAGIC_THRESHOLD * 7);
363 if (filter->mode == 0 || filter->snaptime <= 0) {
364 diff += filter->buf_margin_left;
365 p = filter->blurzoombuf;
366 for (y = 0; y < filter->buf_height; y++) {
367 for (x = 0; x < filter->buf_width; x++) {
368 p[x] |= diff[x] >> 3;
371 p += filter->buf_width;
373 if (filter->mode == 1 || filter->mode == 2) {
374 memcpy (filter->snapframe, src, width * height * 4);
378 blurzoomcore (filter);
380 if (filter->mode == 1 || filter->mode == 2) {
381 src = filter->snapframe;
383 p = filter->blurzoombuf;
384 for (y = 0; y < height; y++) {
385 for (x = 0; x < filter->buf_margin_left; x++) {
388 for (x = 0; x < filter->buf_width; x++) {
389 a = *src++ & 0xfefeff;
393 *dest++ = a | (b - (b >> 8));
395 for (x = 0; x < filter->buf_margin_right; x++) {
400 if (filter->mode == 1 || filter->mode == 2) {
402 if (filter->snaptime < 0) {
403 filter->snaptime = filter->interval;
406 GST_OBJECT_UNLOCK (filter);
408 gst_video_frame_unmap (&in_frame);
409 gst_video_frame_unmap (&out_frame);
416 GST_DEBUG_OBJECT (filter, "invalid input frame");
417 return GST_FLOW_ERROR;
421 GST_DEBUG_OBJECT (filter, "invalid output frame");
422 gst_video_frame_unmap (&in_frame);
423 return GST_FLOW_ERROR;
428 gst_radioactv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
431 GstRadioacTV *filter = GST_RADIOACTV (btrans);
435 if (!gst_video_info_from_caps (&info, incaps))
440 width = GST_VIDEO_INFO_WIDTH (&info);
441 height = GST_VIDEO_INFO_HEIGHT (&info);
443 filter->buf_width_blocks = width / 32;
444 if (filter->buf_width_blocks > 255)
447 filter->buf_width = filter->buf_width_blocks * 32;
448 filter->buf_height = height;
449 filter->buf_area = filter->buf_height * filter->buf_width;
450 filter->buf_margin_left = (width - filter->buf_width) / 2;
451 filter->buf_margin_right =
452 height - filter->buf_width - filter->buf_margin_left;
454 if (filter->blurzoombuf)
455 g_free (filter->blurzoombuf);
456 filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
458 if (filter->blurzoomx)
459 g_free (filter->blurzoomx);
460 filter->blurzoomx = g_new0 (gint, filter->buf_width);
462 if (filter->blurzoomy)
463 g_free (filter->blurzoomy);
464 filter->blurzoomy = g_new0 (gint, filter->buf_height);
466 if (filter->snapframe)
467 g_free (filter->snapframe);
468 filter->snapframe = g_new (guint32, width * height);
471 g_free (filter->diff);
472 filter->diff = g_new (guint8, width * height);
474 if (filter->background)
475 g_free (filter->background);
476 filter->background = g_new0 (gint16, width * height);
485 GST_DEBUG_OBJECT (filter, "invalid caps received");
490 GST_DEBUG_OBJECT (filter, "frame too wide");
496 gst_radioactv_start (GstBaseTransform * trans)
498 GstRadioacTV *filter = GST_RADIOACTV (trans);
500 filter->snaptime = 0;
506 gst_radioactv_finalize (GObject * object)
508 GstRadioacTV *filter = GST_RADIOACTV (object);
510 if (filter->snapframe)
511 g_free (filter->snapframe);
512 filter->snapframe = NULL;
514 if (filter->blurzoombuf)
515 g_free (filter->blurzoombuf);
516 filter->blurzoombuf = NULL;
519 g_free (filter->diff);
522 if (filter->background)
523 g_free (filter->background);
524 filter->background = NULL;
526 if (filter->blurzoomx)
527 g_free (filter->blurzoomx);
528 filter->blurzoomx = NULL;
530 if (filter->blurzoomy)
531 g_free (filter->blurzoomy);
532 filter->blurzoomy = NULL;
534 G_OBJECT_CLASS (parent_class)->finalize (object);
538 gst_radioactv_set_property (GObject * object, guint prop_id,
539 const GValue * value, GParamSpec * pspec)
541 GstRadioacTV *filter = GST_RADIOACTV (object);
543 GST_OBJECT_LOCK (filter);
546 filter->mode = g_value_get_enum (value);
547 if (filter->mode == 3)
548 filter->snaptime = 1;
551 filter->color = g_value_get_enum (value);
554 filter->interval = g_value_get_uint (value);
557 filter->trigger = g_value_get_boolean (value);
560 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
563 GST_OBJECT_UNLOCK (filter);
567 gst_radioactv_get_property (GObject * object, guint prop_id,
568 GValue * value, GParamSpec * pspec)
570 GstRadioacTV *filter = GST_RADIOACTV (object);
574 g_value_set_enum (value, filter->mode);
577 g_value_set_enum (value, filter->color);
580 g_value_set_uint (value, filter->interval);
583 g_value_set_boolean (value, filter->trigger);
586 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
592 gst_radioactv_class_init (GstRadioacTVClass * klass)
594 GObjectClass *gobject_class = (GObjectClass *) klass;
595 GstElementClass *gstelement_class = (GstElementClass *) klass;
596 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
598 gobject_class->set_property = gst_radioactv_set_property;
599 gobject_class->get_property = gst_radioactv_get_property;
601 gobject_class->finalize = gst_radioactv_finalize;
603 g_object_class_install_property (gobject_class, PROP_MODE,
604 g_param_spec_enum ("mode", "Mode",
605 "Mode", GST_TYPE_RADIOACTV_MODE, DEFAULT_MODE,
606 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
608 g_object_class_install_property (gobject_class, PROP_COLOR,
609 g_param_spec_enum ("color", "Color",
610 "Color", GST_TYPE_RADIOACTV_COLOR, DEFAULT_COLOR,
611 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
613 g_object_class_install_property (gobject_class, PROP_INTERVAL,
614 g_param_spec_uint ("interval", "Interval",
615 "Snapshot interval (in strobe mode)", 0, G_MAXINT, DEFAULT_INTERVAL,
616 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
618 g_object_class_install_property (gobject_class, PROP_TRIGGER,
619 g_param_spec_boolean ("trigger", "Trigger",
620 "Trigger (in trigger mode)", DEFAULT_TRIGGER,
621 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
623 gst_element_class_set_details_simple (gstelement_class, "RadioacTV effect",
624 "Filter/Effect/Video",
625 "motion-enlightment effect",
626 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
627 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
629 gst_element_class_add_pad_template (gstelement_class,
630 gst_static_pad_template_get (&gst_radioactv_sink_template));
631 gst_element_class_add_pad_template (gstelement_class,
632 gst_static_pad_template_get (&gst_radioactv_src_template));
634 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_radioactv_set_caps);
635 trans_class->transform = GST_DEBUG_FUNCPTR (gst_radioactv_transform);
636 trans_class->start = GST_DEBUG_FUNCPTR (gst_radioactv_start);
642 gst_radioactv_init (GstRadioacTV * filter)
644 filter->mode = DEFAULT_MODE;
645 filter->color = DEFAULT_COLOR;
646 filter->interval = DEFAULT_INTERVAL;
647 filter->trigger = DEFAULT_TRIGGER;
649 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SRC_PAD (filter));
650 gst_pad_use_fixed_caps (GST_BASE_TRANSFORM_SINK_PAD (filter));