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 *
30 * RadioacTV does *NOT* detect a radioactivity. It detects a difference
31 * from previous frame and blurs it.
32 *
33 * RadioacTV has 4 mode, normal, strobe1, strobe2 and trigger.
34 * In trigger mode, effect appears only when the trigger property is %TRUE.
35 *
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.
39 *
40 * <refsect2>
41 * <title>Example launch line</title>
42 * |[
43 * gst-launch-1.0 -v videotestsrc ! radioactv ! videoconvert ! autovideosink
44 * ]| This pipeline shows the effect of radioactv on a test stream.
45 * </refsect2>
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
139 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
140 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ RGBx, BGRx }")
141 #else
142 #define CAPS_STR GST_VIDEO_CAPS_MAKE ("{ xBGR, xRGB }")
143 #endif
144
145 static GstStaticPadTemplate gst_radioactv_src_template =
146 GST_STATIC_PAD_TEMPLATE ("src",
147 GST_PAD_SRC,
148 GST_PAD_ALWAYS,
149 GST_STATIC_CAPS (CAPS_STR)
150 );
151
152 static GstStaticPadTemplate gst_radioactv_sink_template =
153 GST_STATIC_PAD_TEMPLATE ("sink",
154 GST_PAD_SINK,
155 GST_PAD_ALWAYS,
156 GST_STATIC_CAPS (CAPS_STR)
157 );
158
159 static void
makePalette(void)160 makePalette (void)
161 {
162 gint i;
163
164 #define DELTA (255/(COLORS/2-1))
165
166 /* red, gree, blue */
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;
171 }
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;
178 }
179 /* white */
180 for (i = 0; i < COLORS; i++) {
181 palettes[COLORS * 3 + i] = (255 * i / COLORS) * 0x10101;
182 }
183 for (i = 0; i < COLORS * PATTERN; i++) {
184 palettes[i] = palettes[i] & 0xfefeff;
185 }
186 #undef DELTA
187 }
188
189 #define VIDEO_HWIDTH (filter->buf_width/2)
190 #define VIDEO_HHEIGHT (filter->buf_height/2)
191
192 /* this table assumes that video_width is times of 32 */
193 static void
setTable(GstRadioacTV * filter)194 setTable (GstRadioacTV * filter)
195 {
196 guint bits;
197 gint x, y, tx, ty, xx;
198 gint ptr, prevptr;
199
200 prevptr = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
201 for (xx = 0; xx < (filter->buf_width_blocks); xx++) {
202 bits = 0;
203 for (x = 0; x < 32; x++) {
204 ptr = (gint) (0.5 + RATIO * (xx * 32 + x - VIDEO_HWIDTH) + VIDEO_HWIDTH);
205 bits = bits >> 1;
206 if (ptr != prevptr)
207 bits |= 0x80000000;
208 prevptr = ptr;
209 }
210 filter->blurzoomx[xx] = bits;
211 }
212
213 ty = (gint) (0.5 + RATIO * (-VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
214 tx = (gint) (0.5 + RATIO * (-VIDEO_HWIDTH) + VIDEO_HWIDTH);
215 xx = (gint) (0.5 + RATIO * (filter->buf_width - 1 - VIDEO_HWIDTH) +
216 VIDEO_HWIDTH);
217 filter->blurzoomy[0] = ty * filter->buf_width + tx;
218 prevptr = ty * filter->buf_width + xx;
219 for (y = 1; y < filter->buf_height; y++) {
220 ty = (gint) (0.5 + RATIO * (y - VIDEO_HHEIGHT) + VIDEO_HHEIGHT);
221 filter->blurzoomy[y] = ty * filter->buf_width + tx - prevptr;
222 prevptr = ty * filter->buf_width + xx;
223 }
224 }
225
226 #undef VIDEO_HWIDTH
227 #undef VIDEO_HHEIGHT
228
229 static void
blur(GstRadioacTV * filter)230 blur (GstRadioacTV * filter)
231 {
232 gint x, y;
233 gint width;
234 guint8 *p, *q;
235 guint8 v;
236 GstVideoInfo *info;
237
238 info = &GST_VIDEO_FILTER (filter)->in_info;
239
240 width = filter->buf_width;
241 p = filter->blurzoombuf + GST_VIDEO_INFO_WIDTH (info) + 1;
242 q = p + filter->buf_area;
243
244 for (y = filter->buf_height - 2; y > 0; y--) {
245 for (x = width - 2; x > 0; x--) {
246 v = (*(p - width) + *(p - 1) + *(p + 1) + *(p + width)) / 4 - 1;
247 if (v == 255)
248 v = 0;
249 *q = v;
250 p++;
251 q++;
252 }
253 p += 2;
254 q += 2;
255 }
256 }
257
258 static void
zoom(GstRadioacTV * filter)259 zoom (GstRadioacTV * filter)
260 {
261 gint b, x, y;
262 guint8 *p, *q;
263 gint blocks, height;
264 gint dx;
265
266 p = filter->blurzoombuf + filter->buf_area;
267 q = filter->blurzoombuf;
268 height = filter->buf_height;
269 blocks = filter->buf_width_blocks;
270
271 for (y = 0; y < height; y++) {
272 p += filter->blurzoomy[y];
273 for (b = 0; b < blocks; b++) {
274 dx = filter->blurzoomx[b];
275 for (x = 0; x < 32; x++) {
276 p += (dx & 1);
277 *q++ = *p;
278 dx = dx >> 1;
279 }
280 }
281 }
282 }
283
284 static void
blurzoomcore(GstRadioacTV * filter)285 blurzoomcore (GstRadioacTV * filter)
286 {
287 blur (filter);
288 zoom (filter);
289 }
290
291 /* Background image is refreshed every frame */
292 static void
image_bgsubtract_update_y(guint32 * src,gint16 * background,guint8 * diff,gint video_area,gint y_threshold)293 image_bgsubtract_update_y (guint32 * src, gint16 * background, guint8 * diff,
294 gint video_area, gint y_threshold)
295 {
296 gint i;
297 gint R, G, B;
298 guint32 *p;
299 gint16 *q;
300 guint8 *r;
301 gint v;
302
303 p = src;
304 q = background;
305 r = diff;
306 for (i = 0; i < video_area; i++) {
307 R = ((*p) & 0xff0000) >> (16 - 1);
308 G = ((*p) & 0xff00) >> (8 - 2);
309 B = (*p) & 0xff;
310 v = (R + G + B) - (gint) (*q);
311 *q = (gint16) (R + G + B);
312 *r = ((v + y_threshold) >> 24) | ((y_threshold - v) >> 24);
313
314 p++;
315 q++;
316 r++;
317 }
318 }
319
320 static GstFlowReturn
gst_radioactv_transform_frame(GstVideoFilter * vfilter,GstVideoFrame * in_frame,GstVideoFrame * out_frame)321 gst_radioactv_transform_frame (GstVideoFilter * vfilter,
322 GstVideoFrame * in_frame, GstVideoFrame * out_frame)
323 {
324 GstRadioacTV *filter = GST_RADIOACTV (vfilter);
325 guint32 *src, *dest;
326 GstClockTime timestamp, stream_time;
327 gint x, y, width, height;
328 guint32 a, b;
329 guint8 *diff, *p;
330 guint32 *palette;
331
332 timestamp = GST_BUFFER_TIMESTAMP (in_frame->buffer);
333 stream_time =
334 gst_segment_to_stream_time (&GST_BASE_TRANSFORM (filter)->segment,
335 GST_FORMAT_TIME, timestamp);
336
337 GST_DEBUG_OBJECT (filter, "sync to %" GST_TIME_FORMAT,
338 GST_TIME_ARGS (timestamp));
339
340 if (GST_CLOCK_TIME_IS_VALID (stream_time))
341 gst_object_sync_values (GST_OBJECT (filter), stream_time);
342
343 src = GST_VIDEO_FRAME_PLANE_DATA (in_frame, 0);
344 dest = GST_VIDEO_FRAME_PLANE_DATA (out_frame, 0);
345
346 width = GST_VIDEO_FRAME_WIDTH (in_frame);
347 height = GST_VIDEO_FRAME_HEIGHT (in_frame);
348
349 GST_OBJECT_LOCK (filter);
350 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
351 if (GST_VIDEO_FRAME_FORMAT (in_frame) == GST_VIDEO_FORMAT_RGBx) {
352 palette = &palettes[COLORS * filter->color];
353 } else {
354 palette = &palettes[COLORS * swap_tab[filter->color]];
355 }
356 #else
357 if (GST_VIDEO_FRAME_FORMAT (in_frame) == GST_VIDEO_FORMAT_xBGR) {
358 palette = &palettes[COLORS * filter->color];
359 } else {
360 palette = &palettes[COLORS * swap_tab[filter->color]];
361 }
362 #endif
363 diff = filter->diff;
364
365 if (filter->mode == 3 && filter->trigger)
366 filter->snaptime = 0;
367 else if (filter->mode == 3 && !filter->trigger)
368 filter->snaptime = 1;
369
370 if (filter->mode != 2 || filter->snaptime <= 0) {
371 image_bgsubtract_update_y (src, filter->background, diff,
372 width * height, MAGIC_THRESHOLD * 7);
373 if (filter->mode == 0 || filter->snaptime <= 0) {
374 diff += filter->buf_margin_left;
375 p = filter->blurzoombuf;
376 for (y = 0; y < filter->buf_height; y++) {
377 for (x = 0; x < filter->buf_width; x++) {
378 p[x] |= diff[x] >> 3;
379 }
380 diff += width;
381 p += filter->buf_width;
382 }
383 if (filter->mode == 1 || filter->mode == 2) {
384 memcpy (filter->snapframe, src, width * height * 4);
385 }
386 }
387 }
388 blurzoomcore (filter);
389
390 if (filter->mode == 1 || filter->mode == 2) {
391 src = filter->snapframe;
392 }
393 p = filter->blurzoombuf;
394 for (y = 0; y < height; y++) {
395 for (x = 0; x < filter->buf_margin_left; x++) {
396 *dest++ = *src++;
397 }
398 for (x = 0; x < filter->buf_width; x++) {
399 a = *src++ & 0xfefeff;
400 b = palette[*p++];
401 a += b;
402 b = a & 0x1010100;
403 *dest++ = a | (b - (b >> 8));
404 }
405 for (x = 0; x < filter->buf_margin_right; x++) {
406 *dest++ = *src++;
407 }
408 }
409
410 if (filter->mode == 1 || filter->mode == 2) {
411 filter->snaptime--;
412 if (filter->snaptime < 0) {
413 filter->snaptime = filter->interval;
414 }
415 }
416 GST_OBJECT_UNLOCK (filter);
417
418 return GST_FLOW_OK;
419 }
420
421 static gboolean
gst_radioactv_set_info(GstVideoFilter * vfilter,GstCaps * incaps,GstVideoInfo * in_info,GstCaps * outcaps,GstVideoInfo * out_info)422 gst_radioactv_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
423 GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
424 {
425 GstRadioacTV *filter = GST_RADIOACTV (vfilter);
426 gint width, height;
427
428 width = GST_VIDEO_INFO_WIDTH (in_info);
429 height = GST_VIDEO_INFO_HEIGHT (in_info);
430
431 filter->buf_width_blocks = width / 32;
432 if (filter->buf_width_blocks > 255)
433 goto too_wide;
434
435 filter->buf_width = filter->buf_width_blocks * 32;
436 filter->buf_height = height;
437 filter->buf_area = filter->buf_height * filter->buf_width;
438 filter->buf_margin_left = (width - filter->buf_width) / 2;
439 filter->buf_margin_right =
440 height - filter->buf_width - filter->buf_margin_left;
441
442 g_free (filter->blurzoombuf);
443 filter->blurzoombuf = g_new0 (guint8, filter->buf_area * 2);
444
445 g_free (filter->blurzoomx);
446 filter->blurzoomx = g_new0 (gint, filter->buf_width);
447
448 g_free (filter->blurzoomy);
449 filter->blurzoomy = g_new0 (gint, filter->buf_height);
450
451 g_free (filter->snapframe);
452 filter->snapframe = g_new (guint32, width * height);
453
454 g_free (filter->diff);
455 filter->diff = g_new (guint8, width * height);
456
457 g_free (filter->background);
458 filter->background = g_new0 (gint16, width * height);
459
460 setTable (filter);
461
462 return TRUE;
463
464 /* ERRORS */
465 too_wide:
466 {
467 GST_DEBUG_OBJECT (filter, "frame too wide");
468 return FALSE;
469 }
470 }
471
472 static gboolean
gst_radioactv_start(GstBaseTransform * trans)473 gst_radioactv_start (GstBaseTransform * trans)
474 {
475 GstRadioacTV *filter = GST_RADIOACTV (trans);
476
477 filter->snaptime = 0;
478
479 return TRUE;
480 }
481
482 static void
gst_radioactv_finalize(GObject * object)483 gst_radioactv_finalize (GObject * object)
484 {
485 GstRadioacTV *filter = GST_RADIOACTV (object);
486
487 g_free (filter->snapframe);
488 filter->snapframe = NULL;
489
490 g_free (filter->blurzoombuf);
491 filter->blurzoombuf = NULL;
492
493 g_free (filter->diff);
494 filter->diff = NULL;
495
496 g_free (filter->background);
497 filter->background = NULL;
498
499 g_free (filter->blurzoomx);
500 filter->blurzoomx = NULL;
501
502 g_free (filter->blurzoomy);
503 filter->blurzoomy = NULL;
504
505 G_OBJECT_CLASS (parent_class)->finalize (object);
506 }
507
508 static void
gst_radioactv_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)509 gst_radioactv_set_property (GObject * object, guint prop_id,
510 const GValue * value, GParamSpec * pspec)
511 {
512 GstRadioacTV *filter = GST_RADIOACTV (object);
513
514 GST_OBJECT_LOCK (filter);
515 switch (prop_id) {
516 case PROP_MODE:
517 filter->mode = g_value_get_enum (value);
518 if (filter->mode == 3)
519 filter->snaptime = 1;
520 break;
521 case PROP_COLOR:
522 filter->color = g_value_get_enum (value);
523 break;
524 case PROP_INTERVAL:
525 filter->interval = g_value_get_uint (value);
526 break;
527 case PROP_TRIGGER:
528 filter->trigger = g_value_get_boolean (value);
529 break;
530 default:
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
532 break;
533 }
534 GST_OBJECT_UNLOCK (filter);
535 }
536
537 static void
gst_radioactv_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)538 gst_radioactv_get_property (GObject * object, guint prop_id,
539 GValue * value, GParamSpec * pspec)
540 {
541 GstRadioacTV *filter = GST_RADIOACTV (object);
542
543 switch (prop_id) {
544 case PROP_MODE:
545 g_value_set_enum (value, filter->mode);
546 break;
547 case PROP_COLOR:
548 g_value_set_enum (value, filter->color);
549 break;
550 case PROP_INTERVAL:
551 g_value_set_uint (value, filter->interval);
552 break;
553 case PROP_TRIGGER:
554 g_value_set_boolean (value, filter->trigger);
555 break;
556 default:
557 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
558 break;
559 }
560 }
561
562 static void
gst_radioactv_class_init(GstRadioacTVClass * klass)563 gst_radioactv_class_init (GstRadioacTVClass * klass)
564 {
565 GObjectClass *gobject_class = (GObjectClass *) klass;
566 GstElementClass *gstelement_class = (GstElementClass *) klass;
567 GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
568 GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
569
570 gobject_class->set_property = gst_radioactv_set_property;
571 gobject_class->get_property = gst_radioactv_get_property;
572
573 gobject_class->finalize = gst_radioactv_finalize;
574
575 g_object_class_install_property (gobject_class, PROP_MODE,
576 g_param_spec_enum ("mode", "Mode",
577 "Mode", GST_TYPE_RADIOACTV_MODE, DEFAULT_MODE,
578 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
579
580 g_object_class_install_property (gobject_class, PROP_COLOR,
581 g_param_spec_enum ("color", "Color",
582 "Color", GST_TYPE_RADIOACTV_COLOR, DEFAULT_COLOR,
583 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584
585 g_object_class_install_property (gobject_class, PROP_INTERVAL,
586 g_param_spec_uint ("interval", "Interval",
587 "Snapshot interval (in strobe mode)", 0, G_MAXINT, DEFAULT_INTERVAL,
588 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
589
590 g_object_class_install_property (gobject_class, PROP_TRIGGER,
591 g_param_spec_boolean ("trigger", "Trigger",
592 "Trigger (in trigger mode)", DEFAULT_TRIGGER,
593 GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
594
595 gst_element_class_set_static_metadata (gstelement_class, "RadioacTV effect",
596 "Filter/Effect/Video",
597 "motion-enlightment effect",
598 "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>, "
599 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
600
601 gst_element_class_add_static_pad_template (gstelement_class,
602 &gst_radioactv_sink_template);
603 gst_element_class_add_static_pad_template (gstelement_class,
604 &gst_radioactv_src_template);
605
606 trans_class->start = GST_DEBUG_FUNCPTR (gst_radioactv_start);
607
608 vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_radioactv_set_info);
609 vfilter_class->transform_frame =
610 GST_DEBUG_FUNCPTR (gst_radioactv_transform_frame);
611
612 makePalette ();
613 }
614
615 static void
gst_radioactv_init(GstRadioacTV * filter)616 gst_radioactv_init (GstRadioacTV * filter)
617 {
618 filter->mode = DEFAULT_MODE;
619 filter->color = DEFAULT_COLOR;
620 filter->interval = DEFAULT_INTERVAL;
621 filter->trigger = DEFAULT_TRIGGER;
622 }
623