1 /* GStreamer
2 * Cradioacyright (C) <2009> Sebastian Dröge <sebastian.droege@collabora.co.uk>
3 *
4 * EffecTV - Realtime Digital Video Effector
5 * Cradioacyright (C) 2001-2006 FUKUCHI Kentaro
6 *
7 * RadioacTV - motion-enlightment effect.
8 * Cradioacyright (C) 2001-2002 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 radioaction) any later version.
15 *
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.
20 *
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.
25 */
26
27 /**
28 * SECTION:element-radioactv
29 * @title: radioactv
30 *
31 * RadioacTV does *NOT* detect a radioactivity. It detects a difference
32 * from previous frame and blurs it.
33 *
34 * RadioacTV has 4 mode, normal, strobe1, strobe2 and trigger.
35 * In trigger mode, effect appears only when the trigger property is %TRUE.
36 *
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.
40 *
41 * ## Example launch line
42 * |[
43 * gst-launch-1.0 -v videotestsrc ! radioactv ! videoconvert ! autovideosink
44 * ]| This pipeline shows the effect of radioactv on a test stream.
45 *
46 */
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include <math.h>
53 #include <string.h>
54
55 #include "gstradioac.h"
56 #include "gsteffectv.h"
57
58 enum
59 {
60 RADIOAC_NORMAL = 0,
61 RADIOAC_STROBE,
62 RADIOAC_STROBE2,
63 RADIOAC_TRIGGER
64 };
65
66 enum
67 {
68 COLOR_RED = 0,
69 COLOR_GREEN,
70 COLOR_BLUE,
71 COLOR_WHITE
72 };
73
74 #define GST_TYPE_RADIOACTV_MODE (gst_radioactv_mode_get_type())
75 static GType
gst_radioactv_mode_get_type(void)76 gst_radioactv_mode_get_type (void)
77 {
78 static GType type = 0;
79
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"},
85 {0, NULL, NULL},
86 };
87
88 if (!type) {
89 type = g_enum_register_static ("GstRadioacTVMode", enumvalue);
90 }
91 return type;
92 }
93
94 #define GST_TYPE_RADIOACTV_COLOR (gst_radioactv_color_get_type())
95 static GType
gst_radioactv_color_get_type(void)96 gst_radioactv_color_get_type (void)
97 {
98 static GType type = 0;
99
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"},
105 {0, NULL, NULL},
106 };
107
108 if (!type) {
109 type = g_enum_register_static ("GstRadioacTVColor", enumvalue);
110 }
111 return type;
112 }
113
114 #define DEFAULT_MODE RADIOAC_NORMAL
115 #define DEFAULT_COLOR COLOR_WHITE
116 #define DEFAULT_INTERVAL 3
117 #define DEFAULT_TRIGGER FALSE
118
119 enum
120 {
121 PROP_0,
122 PROP_MODE,
123 PROP_COLOR,
124 PROP_INTERVAL,
125 PROP_TRIGGER
126 };
127
128 #define COLORS 32
129 #define PATTERN 4
130 #define MAGIC_THRESHOLD 40
131 #define RATIO 0.95
132
133 static guint32 palettes[COLORS * PATTERN];
134 static const gint swap_tab[] = { 2, 1, 0, 3 };
135
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,
139 GST_TYPE_RADIOACTV);
140
141 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
142 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ RGBx, BGRx }")
143 #else
144 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
145 #endif
146
147 static GstStaticPadTemplate gst_radioactv_src_template =
148 GST_STATIC_PAD_TEMPLATE ("src",
149 GST_PAD_SRC,
150 GST_PAD_ALWAYS,
151 GST_STATIC_CAPS (CAPS_STR)
152 );
153
154 static GstStaticPadTemplate gst_radioactv_sink_template =
155 GST_STATIC_PAD_TEMPLATE ("sink",
156 GST_PAD_SINK,
157 GST_PAD_ALWAYS,
158 GST_STATIC_CAPS (CAPS_STR)
159 );
160
161 static void
makePalette(void)162 makePalette (void)
163 {
164 gint i;
165
166 #define DELTA (255/(COLORS/2-1))
167
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;
173 }
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;
180 }
181 /* white */
182 for (i = 0; i < COLORS; i++) {
183 palettes[COLORS * 3 + i] = (255 * i / COLORS) * 0x10101;
184 }
185 for (i = 0; i < COLORS * PATTERN; i++) {
186 palettes[i] = palettes[i] & 0xfefeff;
187 }
188 #undef DELTA
189 }
190
191 #define VIDEO_HWIDTH (filter->buf_width/2)
192 #define VIDEO_HHEIGHT (filter->buf_height/2)
193
194 /* this table assumes that video_width is times of 32 */
195 static void
setTable(GstRadioacTV * filter)196 setTable (GstRadioacTV * filter)
197 {
198 guint bits;
199 gint x, y, tx, ty, xx;
200 gint ptr, prevptr;
201
202 prevptr = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
203 for (xx = 0; xx < (filter->buf_width_blocks); xx++) {
204 bits = 0;
205 for (x = 0; x < 32; x++) {
206 ptr = (gint) (0.5 + RATIO * (xx * 32 + x - VIDEO_HWIDTH) + VIDEO_HWIDTH);
207 bits = bits >> 1;
208 if (ptr != prevptr)
209 bits |= 0x80000000;
210 prevptr = ptr;
211 }
212 filter->blurzoomx[xx] = bits;
213 }
214
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) +
218 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;
225 }
226 }
227
228 #undef VIDEO_HWIDTH
229 #undef VIDEO_HHEIGHT
230
231 static void
blur(GstRadioacTV * filter)232 blur (GstRadioacTV * filter)
233 {
234 gint x, y;
235 gint width;
236 guint8 *p, *q;
237 guint8 v;
238 GstVideoInfo *info;
239
240 info = &GST_VIDEO_FILTER (filter)->in_info;
241
242 width = filter->buf_width;
243 p = filter->blurzoombuf + GST_VIDEO_INFO_WIDTH (info) + 1;
244 q = p + filter->buf_area;
245
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;
249 if (v == 255)
250 v = 0;
251 *q = v;
252 p++;
253 q++;
254 }
255 p += 2;
256 q += 2;
257 }
258 }
259
260 static void
zoom(GstRadioacTV * filter)261 zoom (GstRadioacTV * filter)
262 {
263 gint b, x, y;
264 guint8 *p, *q;
265 gint blocks, height;
266 gint dx;
267
268 p = filter->blurzoombuf + filter->buf_area;
269 q = filter->blurzoombuf;
270 height = filter->buf_height;
271 blocks = filter->buf_width_blocks;
272
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++) {
278 p += (dx & 1);
279 *q++ = *p;
280 dx = dx >> 1;
281 }
282 }
283 }
284 }
285
286 static void
blurzoomcore(GstRadioacTV * filter)287 blurzoomcore (GstRadioacTV * filter)
288 {
289 blur (filter);
290 zoom (filter);
291 }
292
293 /* Background image is refreshed every frame */
294 static void
image_bgsubtract_update_y(guint32 * src,gint16 * background,guint8 * diff,gint video_area,gint y_threshold)295 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
296 gint video_area, gint y_threshold)
297 {
298 gint i;
299 gint R, G, B;
300 guint32 *p;
301 gint16 *q;
302 guint8 *r;
303 gint v;
304
305 p = src;
306 q = background;
307 r = diff;
308 for (i = 0; i < video_area; i++) {
309 R = ((*p) & 0xff0000) >> (16 - 1);
310 G = ((*p) & 0xff00) >> (8 - 2);
311 B = (*p) & 0xff;
312 v = (R + G + B) - (gint) (*q);
313 *q = (gint16) (R + G + B);
314 *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24);
315
316 p++;
317 q++;
318 r++;
319 }
320 }
321
322 static GstFlowReturn
gst_radioactv_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)323 gst_radioactv_transform_frame (GstVideoFilter * vfilter,
324 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
325 {
326 GstRadioacTV *filter = GST_RADIOACTV (vfilter);
327 guint32 *src, *dest;
328 GstClockTime timestamp, stream_time;
329 gint x, y, width, height;
330 guint32 a, b;
331 guint8 *diff, *p;
332 guint32 *palette;
333
334 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
335 stream_time =
336 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
337 GST_FORMAT_TIME, timestamp);
338
339 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
340 GST_TIME_ARGS (timestamp));
341
342 if (GST_CLOCK_TIME_IS_VALID (stream_time))
343 gst_object_sync_values (GST_OBJECT (filter), stream_time);
344
345 src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
346 dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
347
348 width = GST_VIDEO_FRAME_WIDTH (in_frame);
349 height = GST_VIDEO_FRAME_HEIGHT (in_frame);
350
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];
355 } else {
356 palette = &palettes[COLORS * swap_tab[filter->color]];
357 }
358 #else
359 if (GST_VIDEO_FRAME_FORMAT (in_frame) == GST_VIDEO_FORMAT_xBGR) {
360 palette = &palettes[COLORS * filter->color];
361 } else {
362 palette = &palettes[COLORS * swap_tab[filter->color]];
363 }
364 #endif
365 diff = filter->diff;
366
367 if (filter->mode == 3 && filter->trigger)
368 filter->snaptime = 0;
369 else if (filter->mode == 3 && !filter->trigger)
370 filter->snaptime = 1;
371
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;
381 }
382 diff += width;
383 p += filter->buf_width;
384 }
385 if (filter->mode == 1 || filter->mode == 2) {
386 memcpy (filter->snapframe, src, width * height * 4);
387 }
388 }
389 }
390 blurzoomcore (filter);
391
392 if (filter->mode == 1 || filter->mode == 2) {
393 src = filter->snapframe;
394 }
395 p = filter->blurzoombuf;
396 for (y = 0; y < height; y++) {
397 for (x = 0; x < filter->buf_margin_left; x++) {
398 *dest++ = *src++;
399 }
400 for (x = 0; x < filter->buf_width; x++) {
401 a = *src++ & 0xfefeff;
402 b = palette[*p++];
403 a += b;
404 b = a & 0x1010100;
405 *dest++ = a | (b - (b >> 8));
406 }
407 for (x = 0; x < filter->buf_margin_right; x++) {
408 *dest++ = *src++;
409 }
410 }
411
412 if (filter->mode == 1 || filter->mode == 2) {
413 filter->snaptime--;
414 if (filter->snaptime < 0) {
415 filter->snaptime = filter->interval;
416 }
417 }
418 GST_OBJECT_UNLOCK (filter);
419
420 return GST_FLOW_OK;
421 }
422
423 static gboolean
gst_radioactv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)424 gst_radioactv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
425 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
426 {
427 GstRadioacTV *filter = GST_RADIOACTV (vfilter);
428 gint width, height;
429
430 width = GST_VIDEO_INFO_WIDTH (in_info);
431 height = GST_VIDEO_INFO_HEIGHT (in_info);
432
433 filter->buf_width_blocks = width / 32;
434 if (filter->buf_width_blocks > 255)
435 goto too_wide;
436
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;
443
444 g_free (filter->blurzoombuf);
445 filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
446
447 g_free (filter->blurzoomx);
448 filter->blurzoomx = g_new0 (gint, filter->buf_width);
449
450 g_free (filter->blurzoomy);
451 filter->blurzoomy = g_new0 (gint, filter->buf_height);
452
453 g_free (filter->snapframe);
454 filter->snapframe = g_new (guint32, width * height);
455
456 g_free (filter->diff);
457 filter->diff = g_new (guint8, width * height);
458
459 g_free (filter->background);
460 filter->background = g_new0 (gint16, width * height);
461
462 setTable (filter);
463
464 return TRUE;
465
466 /* ERRORS */
467 too_wide:
468 {
469 GST_DEBUG_OBJECT (filter, "frame too wide");
470 return FALSE;
471 }
472 }
473
474 static gboolean
gst_radioactv_start(GstBaseTransform * trans)475 gst_radioactv_start (GstBaseTransform * trans)
476 {
477 GstRadioacTV *filter = GST_RADIOACTV (trans);
478
479 filter->snaptime = 0;
480
481 return TRUE;
482 }
483
484 static void
gst_radioactv_finalize(GObject * object)485 gst_radioactv_finalize (GObject * object)
486 {
487 GstRadioacTV *filter = GST_RADIOACTV (object);
488
489 g_free (filter->snapframe);
490 filter->snapframe = NULL;
491
492 g_free (filter->blurzoombuf);
493 filter->blurzoombuf = NULL;
494
495 g_free (filter->diff);
496 filter->diff = NULL;
497
498 g_free (filter->background);
499 filter->background = NULL;
500
501 g_free (filter->blurzoomx);
502 filter->blurzoomx = NULL;
503
504 g_free (filter->blurzoomy);
505 filter->blurzoomy = NULL;
506
507 G_OBJECT_CLASS (parent_class)->finalize (object);
508 }
509
510 static void
gst_radioactv_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)511 gst_radioactv_set_property (GObject * object, guint prop_id,
512 const GValue * value, GParamSpec * pspec)
513 {
514 GstRadioacTV *filter = GST_RADIOACTV (object);
515
516 GST_OBJECT_LOCK (filter);
517 switch (prop_id) {
518 case PROP_MODE:
519 filter->mode = g_value_get_enum (value);
520 if (filter->mode == 3)
521 filter->snaptime = 1;
522 break;
523 case PROP_COLOR:
524 filter->color = g_value_get_enum (value);
525 break;
526 case PROP_INTERVAL:
527 filter->interval = g_value_get_uint (value);
528 break;
529 case PROP_TRIGGER:
530 filter->trigger = g_value_get_boolean (value);
531 break;
532 default:
533 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
534 break;
535 }
536 GST_OBJECT_UNLOCK (filter);
537 }
538
539 static void
gst_radioactv_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)540 gst_radioactv_get_property (GObject * object, guint prop_id,
541 GValue * value, GParamSpec * pspec)
542 {
543 GstRadioacTV *filter = GST_RADIOACTV (object);
544
545 switch (prop_id) {
546 case PROP_MODE:
547 g_value_set_enum (value, filter->mode);
548 break;
549 case PROP_COLOR:
550 g_value_set_enum (value, filter->color);
551 break;
552 case PROP_INTERVAL:
553 g_value_set_uint (value, filter->interval);
554 break;
555 case PROP_TRIGGER:
556 g_value_set_boolean (value, filter->trigger);
557 break;
558 default:
559 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
560 break;
561 }
562 }
563
564 static void
gst_radioactv_class_init(GstRadioacTVClass * klass)565 gst_radioactv_class_init (GstRadioacTVClass * klass)
566 {
567 GObjectClass *gobject_class = (GObjectClass *) klass;
568 GstElementClass *gstelement_class = (GstElementClass *) klass;
569 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
570 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
571
572 gobject_class->set_property = gst_radioactv_set_property;
573 gobject_class->get_property = gst_radioactv_get_property;
574
575 gobject_class->finalize = gst_radioactv_finalize;
576
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));
581
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));
586
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));
591
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));
596
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>");
602
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);
607
608 trans_class->start = GST_DEBUG_FUNCPTR (gst_radioactv_start);
609
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);
613
614 makePalette ();
615
616 gst_type_mark_as_plugin_api (GST_TYPE_RADIOACTV_MODE, 0);
617 gst_type_mark_as_plugin_api (GST_TYPE_RADIOACTV_COLOR, 0);
618 }
619
620 static void
gst_radioactv_init(GstRadioacTV * filter)621 gst_radioactv_init (GstRadioacTV * filter)
622 {
623 filter->mode = DEFAULT_MODE;
624 filter->color = DEFAULT_COLOR;
625 filter->interval = DEFAULT_INTERVAL;
626 filter->trigger = DEFAULT_TRIGGER;
627 }
628