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., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
28 * SECTION:element-radioactv
31 * RadioacTV does *NOT* detect a radioactivity. It detects a difference
32 * from previous frame and blurs it.
34 * RadioacTV has 4 mode, normal, strobe1, strobe2 and trigger.
35 * In trigger mode, effect appears only when the trigger property is %TRUE.
37 * strobe1 and strobe2 mode drops some frames. strobe1 mode uses the difference between
38 * current frame and previous frame dropped, while strobe2 mode uses the difference from
39 * previous frame displayed. The effect of strobe2 is stronger than strobe1.
41 * ## Example launch line
43 * gst-launch-1.0 -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"
74 #define GST_TYPE_RADIOACTV_MODE (gst_radioactv_mode_get_type())
76 gst_radioactv_mode_get_type (void)
78 static GType type = 0;
80 static const GEnumValue enumvalue[] = {
81 {RADIOAC_NORMAL, "Normal", "normal"},
82 {RADIOAC_STROBE, "Strobe 1", "strobe1"},
83 {RADIOAC_STROBE2, "Strobe 2", "strobe2"},
84 {RADIOAC_TRIGGER, "Trigger", "trigger"},
89 type = g_enum_register_static ("GstRadioacTVMode", enumvalue);
94 #define GST_TYPE_RADIOACTV_COLOR (gst_radioactv_color_get_type())
96 gst_radioactv_color_get_type (void)
98 static GType type = 0;
100 static const GEnumValue enumvalue[] = {
101 {COLOR_RED, "Red", "red"},
102 {COLOR_GREEN, "Green", "green"},
103 {COLOR_BLUE, "Blue", "blue"},
104 {COLOR_WHITE, "White", "white"},
109 type = g_enum_register_static ("GstRadioacTVColor", enumvalue);
114 #define DEFAULT_MODE RADIOAC_NORMAL
115 #define DEFAULT_COLOR COLOR_WHITE
116 #define DEFAULT_INTERVAL 3
117 #define DEFAULT_TRIGGER FALSE
130 #define MAGIC_THRESHOLD 40
133 static guint32 palettes[COLORS * PATTERN];
134 static const gint swap_tab[] = { 2, 1, 0, 3 };
136 #define gst_radioactv_parent_class parent_class
137 G_DEFINE_TYPE (GstRadioacTV, gst_radioactv, GST_TYPE_VIDEO_FILTER);
138 GST_ELEMENT_REGISTER_DEFINE (radioactv, "radioactv", GST_RANK_NONE,
141 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
142 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ RGBx, BGRx }")
144 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
147 static GstStaticPadTemplate gst_radioactv_src_template =
148 GST_STATIC_PAD_TEMPLATE ("src",
151 GST_STATIC_CAPS (CAPS_STR)
154 static GstStaticPadTemplate gst_radioactv_sink_template =
155 GST_STATIC_PAD_TEMPLATE ("sink",
158 GST_STATIC_CAPS (CAPS_STR)
166 #define DELTA (255/(COLORS/2-1))
168 /* red, gree, blue */
169 for (i = 0; i < COLORS / 2; i++) {
170 palettes[i] = i * DELTA;
171 palettes[COLORS + i] = (i * DELTA) << 8;
172 palettes[COLORS * 2 + i] = (i * DELTA) << 16;
174 for (i = 0; i < COLORS / 2; i++) {
175 palettes[i + COLORS / 2] = 255 | (i * DELTA) << 16 | (i * DELTA) << 8;
176 palettes[COLORS + i + COLORS / 2] =
177 (255 << 8) | (i * DELTA) << 16 | i * DELTA;
178 palettes[COLORS * 2 + i + COLORS / 2] =
179 (255 << 16) | (i * DELTA) << 8 | i * DELTA;
182 for (i = 0; i < COLORS; i++) {
183 palettes[COLORS * 3 + i] = (255 * i / COLORS) * 0x10101;
185 for (i = 0; i < COLORS * PATTERN; i++) {
186 palettes[i] = palettes[i] & 0xfefeff;
191 #define VIDEO_HWIDTH (filter->buf_width/2)
192 #define VIDEO_HHEIGHT (filter->buf_height/2)
194 /* this table assumes that video_width is times of 32 */
196 setTable (GstRadioacTV * filter)
199 gint x, y, tx, ty, xx;
202 prevptr = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
203 for (xx = 0; xx < (filter->buf_width_blocks); xx++) {
205 for (x = 0; x < 32; x++) {
206 ptr = (gint) (0.5 + RATIO * (xx * 32 + x - VIDEO_HWIDTH) + VIDEO_HWIDTH);
212 filter->blurzoomx[xx] = bits;
215 ty = (gint) (0.5 + RATIO * (-VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
216 tx = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
217 xx = (gint) (0.5 + RATIO * (filter->buf_width - 1 - VIDEO_HWIDTH) +
219 filter->blurzoomy[0] = ty * filter->buf_width + tx;
220 prevptr = ty * filter->buf_width + xx;
221 for (y = 1; y < filter->buf_height; y++) {
222 ty = (gint) (0.5 + RATIO * (y - VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
223 filter->blurzoomy[y] = ty * filter->buf_width + tx - prevptr;
224 prevptr = ty * filter->buf_width + xx;
232 blur (GstRadioacTV * filter)
240 info = &GST_VIDEO_FILTER (filter)->in_info;
242 width = filter->buf_width;
243 p = filter->blurzoombuf + GST_VIDEO_INFO_WIDTH (info) + 1;
244 q = p + filter->buf_area;
246 for (y = filter->buf_height - 2; y > 0; y--) {
247 for (x = width - 2; x > 0; x--) {
248 v = (*(p - width) + *(p - 1) + *(p + 1) + *(p + width)) / 4 - 1;
261 zoom (GstRadioacTV * filter)
268 p = filter->blurzoombuf + filter->buf_area;
269 q = filter->blurzoombuf;
270 height = filter->buf_height;
271 blocks = filter->buf_width_blocks;
273 for (y = 0; y < height; y++) {
274 p += filter->blurzoomy[y];
275 for (b = 0; b < blocks; b++) {
276 dx = filter->blurzoomx[b];
277 for (x = 0; x < 32; x++) {
287 blurzoomcore (GstRadioacTV * filter)
293 /* Background image is refreshed every frame */
295 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
296 gint video_area, gint y_threshold)
308 for (i = 0; i < video_area; i++) {
309 R = ((*p) & 0xff0000) >> (16 - 1);
310 G = ((*p) & 0xff00) >> (8 - 2);
312 v = (R + G + B) - (gint) (*q);
313 *q = (gint16) (R + G + B);
314 *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24);
323 gst_radioactv_transform_frame (GstVideoFilter * vfilter,
324 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
326 GstRadioacTV *filter = GST_RADIOACTV (vfilter);
328 GstClockTime timestamp, stream_time;
329 gint x, y, width, height;
334 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
336 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
337 GST_FORMAT_TIME, timestamp);
339 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
340 GST_TIME_ARGS (timestamp));
342 if (GST_CLOCK_TIME_IS_VALID (stream_time))
343 gst_object_sync_values (GST_OBJECT (filter), stream_time);
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 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
353 if (GST_VIDEO_FRAME_FORMAT (in_frame) == GST_VIDEO_FORMAT_RGBx) {
354 palette = &palettes[COLORS * filter->color];
356 palette = &palettes[COLORS * swap_tab[filter->color]];
359 if (GST_VIDEO_FRAME_FORMAT (in_frame) == GST_VIDEO_FORMAT_xBGR) {
360 palette = &palettes[COLORS * filter->color];
362 palette = &palettes[COLORS * swap_tab[filter->color]];
367 if (filter->mode == 3 && filter->trigger)
368 filter->snaptime = 0;
369 else if (filter->mode == 3 && !filter->trigger)
370 filter->snaptime = 1;
372 if (filter->mode != 2 || filter->snaptime <= 0) {
373 image_bgsubtract_update_y (src, filter->background, diff,
374 width * height, MAGIC_THRESHOLD * 7);
375 if (filter->mode == 0 || filter->snaptime <= 0) {
376 diff += filter->buf_margin_left;
377 p = filter->blurzoombuf;
378 for (y = 0; y < filter->buf_height; y++) {
379 for (x = 0; x < filter->buf_width; x++) {
380 p[x] |= diff[x] >> 3;
383 p += filter->buf_width;
385 if (filter->mode == 1 || filter->mode == 2) {
386 memcpy (filter->snapframe, src, width * height * 4);
390 blurzoomcore (filter);
392 if (filter->mode == 1 || filter->mode == 2) {
393 src = filter->snapframe;
395 p = filter->blurzoombuf;
396 for (y = 0; y < height; y++) {
397 for (x = 0; x < filter->buf_margin_left; x++) {
400 for (x = 0; x < filter->buf_width; x++) {
401 a = *src++ & 0xfefeff;
405 *dest++ = a | (b - (b >> 8));
407 for (x = 0; x < filter->buf_margin_right; x++) {
412 if (filter->mode == 1 || filter->mode == 2) {
414 if (filter->snaptime < 0) {
415 filter->snaptime = filter->interval;
418 GST_OBJECT_UNLOCK (filter);
424 gst_radioactv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
425 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
427 GstRadioacTV *filter = GST_RADIOACTV (vfilter);
430 width = GST_VIDEO_INFO_WIDTH (in_info);
431 height = GST_VIDEO_INFO_HEIGHT (in_info);
433 filter->buf_width_blocks = width / 32;
434 if (filter->buf_width_blocks > 255)
437 filter->buf_width = filter->buf_width_blocks * 32;
438 filter->buf_height = height;
439 filter->buf_area = filter->buf_height * filter->buf_width;
440 filter->buf_margin_left = (width - filter->buf_width) / 2;
441 filter->buf_margin_right =
442 height - filter->buf_width - filter->buf_margin_left;
444 g_free (filter->blurzoombuf);
445 filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
447 g_free (filter->blurzoomx);
448 filter->blurzoomx = g_new0 (gint, filter->buf_width);
450 g_free (filter->blurzoomy);
451 filter->blurzoomy = g_new0 (gint, filter->buf_height);
453 g_free (filter->snapframe);
454 filter->snapframe = g_new (guint32, width * height);
456 g_free (filter->diff);
457 filter->diff = g_new (guint8, width * height);
459 g_free (filter->background);
460 filter->background = g_new0 (gint16, width * height);
469 GST_DEBUG_OBJECT (filter, "frame too wide");
475 gst_radioactv_start (GstBaseTransform * trans)
477 GstRadioacTV *filter = GST_RADIOACTV (trans);
479 filter->snaptime = 0;
485 gst_radioactv_finalize (GObject * object)
487 GstRadioacTV *filter = GST_RADIOACTV (object);
489 g_free (filter->snapframe);
490 filter->snapframe = NULL;
492 g_free (filter->blurzoombuf);
493 filter->blurzoombuf = NULL;
495 g_free (filter->diff);
498 g_free (filter->background);
499 filter->background = NULL;
501 g_free (filter->blurzoomx);
502 filter->blurzoomx = NULL;
504 g_free (filter->blurzoomy);
505 filter->blurzoomy = NULL;
507 G_OBJECT_CLASS (parent_class)->finalize (object);
511 gst_radioactv_set_property (GObject * object, guint prop_id,
512 const GValue * value, GParamSpec * pspec)
514 GstRadioacTV *filter = GST_RADIOACTV (object);
516 GST_OBJECT_LOCK (filter);
519 filter->mode = g_value_get_enum (value);
520 if (filter->mode == 3)
521 filter->snaptime = 1;
524 filter->color = g_value_get_enum (value);
527 filter->interval = g_value_get_uint (value);
530 filter->trigger = g_value_get_boolean (value);
533 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
536 GST_OBJECT_UNLOCK (filter);
540 gst_radioactv_get_property (GObject * object, guint prop_id,
541 GValue * value, GParamSpec * pspec)
543 GstRadioacTV *filter = GST_RADIOACTV (object);
547 g_value_set_enum (value, filter->mode);
550 g_value_set_enum (value, filter->color);
553 g_value_set_uint (value, filter->interval);
556 g_value_set_boolean (value, filter->trigger);
559 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
565 gst_radioactv_class_init (GstRadioacTVClass * klass)
567 GObjectClass *gobject_class = (GObjectClass *) klass;
568 GstElementClass *gstelement_class = (GstElementClass *) klass;
569 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
570 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
572 gobject_class->set_property = gst_radioactv_set_property;
573 gobject_class->get_property = gst_radioactv_get_property;
575 gobject_class->finalize = gst_radioactv_finalize;
577 g_object_class_install_property (gobject_class, PROP_MODE,
578 g_param_spec_enum ("mode", "Mode",
579 "Mode", GST_TYPE_RADIOACTV_MODE, DEFAULT_MODE,
580 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
582 g_object_class_install_property (gobject_class, PROP_COLOR,
583 g_param_spec_enum ("color", "Color",
584 "Color", GST_TYPE_RADIOACTV_COLOR, DEFAULT_COLOR,
585 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
587 g_object_class_install_property (gobject_class, PROP_INTERVAL,
588 g_param_spec_uint ("interval", "Interval",
589 "Snapshot interval (in strobe mode)", 0, G_MAXINT, DEFAULT_INTERVAL,
590 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
592 g_object_class_install_property (gobject_class, PROP_TRIGGER,
593 g_param_spec_boolean ("trigger", "Trigger",
594 "Trigger (in trigger mode)", DEFAULT_TRIGGER,
595 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
597 gst_element_class_set_static_metadata (gstelement_class, "RadioacTV effect",
598 "Filter/Effect/Video",
599 "motion-enlightment effect",
600 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
601 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
603 gst_element_class_add_static_pad_template (gstelement_class,
604 &gst_radioactv_sink_template);
605 gst_element_class_add_static_pad_template (gstelement_class,
606 &gst_radioactv_src_template);
608 trans_class->start = GST_DEBUG_FUNCPTR (gst_radioactv_start);
610 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_radioactv_set_info);
611 vfilter_class->transform_frame =
612 GST_DEBUG_FUNCPTR (gst_radioactv_transform_frame);
616 gst_type_mark_as_plugin_api (GST_TYPE_RADIOACTV_MODE, 0);
617 gst_type_mark_as_plugin_api (GST_TYPE_RADIOACTV_COLOR, 0);
621 gst_radioactv_init (GstRadioacTV * filter)
623 filter->mode = DEFAULT_MODE;
624 filter->color = DEFAULT_COLOR;
625 filter->interval = DEFAULT_INTERVAL;
626 filter->trigger = DEFAULT_TRIGGER;