2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
5 * Copyright (C) 2001 FUKUCHI Kentarou
7 * EffecTV is free software. * This library is free software;
8 * you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 #include "gsteffectv.h"
27 #define GST_TYPE_AGINGTV \
28 (gst_agingtv_get_type())
29 #define GST_AGINGTV(obj) \
30 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AGINGTV,GstAgingTV))
31 #define GST_AGINGTV_CLASS(klass) \
32 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstAgingTV))
33 #define GST_IS_AGINGTV(obj) \
34 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AGINGTV))
35 #define GST_IS_AGINGTV_CLASS(obj) \
36 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AGINGTV))
38 #define SCRATCH_MAX 20
39 typedef struct _scratch
47 static int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1};
48 static int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
51 typedef struct _GstAgingTV GstAgingTV;
52 typedef struct _GstAgingTVClass GstAgingTVClass;
58 GstPad *sinkpad, *srcpad;
65 scratch scratches[SCRATCH_MAX];
72 struct _GstAgingTVClass
74 GstElementClass parent_class;
77 /* elementfactory information */
78 GstElementDetails gst_agingtv_details = {
80 "Filter/Video/Effect",
82 "Aply aging effect on video",
84 "Wim Taymans <wim.taymans@chello.be>",
85 "(C) 2001 FUKUCHI Kentarou",
89 /* Filter signals and args */
101 static void gst_agingtv_class_init (GstAgingTVClass * klass);
102 static void gst_agingtv_init (GstAgingTV * filter);
104 static void aging_mode_switch (GstAgingTV *filter);
106 static void gst_agingtv_set_property (GObject * object, guint prop_id,
107 const GValue * value, GParamSpec * pspec);
108 static void gst_agingtv_get_property (GObject * object, guint prop_id,
109 GValue * value, GParamSpec * pspec);
111 static void gst_agingtv_chain (GstPad * pad, GstBuffer * buf);
113 static GstElementClass *parent_class = NULL;
114 /*static guint gst_agingtv_signals[LAST_SIGNAL] = { 0 }; */
116 GType gst_agingtv_get_type (void)
118 static GType agingtv_type = 0;
121 static const GTypeInfo agingtv_info = {
122 sizeof (GstAgingTVClass), NULL,
124 (GClassInitFunc) gst_agingtv_class_init,
129 (GInstanceInitFunc) gst_agingtv_init,
132 agingtv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAgingTV", &agingtv_info, 0);
138 gst_agingtv_class_init (GstAgingTVClass * klass)
140 GObjectClass *gobject_class;
141 GstElementClass *gstelement_class;
143 gobject_class = (GObjectClass *) klass;
144 gstelement_class = (GstElementClass *) klass;
146 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
148 gobject_class->set_property = gst_agingtv_set_property;
149 gobject_class->get_property = gst_agingtv_get_property;
152 static GstPadConnectReturn
153 gst_agingtv_sinkconnect (GstPad * pad, GstCaps * caps)
157 filter = GST_AGINGTV (gst_pad_get_parent (pad));
159 if (!GST_CAPS_IS_FIXED (caps))
160 return GST_PAD_CONNECT_DELAYED;
162 gst_caps_get_int (caps, "width", &filter->width);
163 gst_caps_get_int (caps, "height", &filter->height);
165 filter->video_size = filter->width * filter->height;
166 filter->aging_mode = 0;
167 aging_mode_switch (filter);
169 return gst_pad_try_set_caps (filter->srcpad, caps);
173 gst_agingtv_init (GstAgingTV * filter)
175 filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
176 gst_pad_set_chain_function (filter->sinkpad, gst_agingtv_chain);
177 gst_pad_set_connect_function (filter->sinkpad, gst_agingtv_sinkconnect);
178 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
180 filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
181 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
188 static unsigned int fastrand_val;
190 return (fastrand_val = fastrand_val * 1103515245 + 12345);
195 coloraging (guint32 *src, guint32 *dest, gint video_area)
200 for (i = video_area; i; i--) {
202 b = (a & 0xfcfcfc) >> 2;
203 *dest++ = a - b + 0x181818 + ((fastrand () >> 8) & 0x101010);
209 scratching (scratch *scratches, gint scratch_lines, guint32 *dest, gint width, gint height)
215 for (i = 0; i < scratch_lines; i++) {
216 scratch = &scratches[i];
219 scratch->x = scratch->x + scratch->dx;
221 if (scratch->x < 0 || scratch->x > width * 256) {
225 p = dest + (scratch->x >> 8);
236 y2 = fastrand () % height;
238 for (y = y1; y < y2; y++) {
242 *p = a | (b - (b >> 8));
246 if ((fastrand () & 0xf0000000) == 0) {
247 scratch->life = 2 + (fastrand () >> 27);
248 scratch->x = fastrand () % (width * 256);
249 scratch->dx = ((int) fastrand ()) >> 23;
250 scratch->init = (fastrand () % (height - 1)) + 1;
257 dusts (guint32 *dest, gint width, gint height, gint dust_interval, gint area_scale)
264 if (dust_interval == 0) {
265 if ((fastrand () & 0xf0000000) == 0) {
266 dust_interval = fastrand () >> 29;
270 dnum = area_scale * 4 + (fastrand() >> 27);
272 for (i = 0; i < dnum; i++) {
273 x = fastrand () % width;
274 y = fastrand () % height;
275 d = fastrand () >> 29;
276 len = fastrand () % area_scale + 5;
277 for (j = 0; j < len; j++) {
278 dest[y * width + x] = 0x101010;
282 if (y >= height || x >= width) break;
284 d = (d + fastrand () % 3 - 1) & 7;
291 pits (guint32 *dest, gint width, gint height, gint area_scale, gint pits_interval)
294 int pnum, size, pnumscale;
297 pnumscale = area_scale * 2;
299 pnum = pnumscale + (fastrand () % pnumscale);
303 pnum = fastrand () % pnumscale;
305 if ((fastrand () & 0xf8000000) == 0) {
306 pits_interval = (fastrand () >> 28) + 20;
309 for (i = 0; i < pnum; i++) {
310 x = fastrand () % (width - 1);
311 y = fastrand () % (height - 1);
313 size = fastrand () >> 28;
315 for (j = 0; j < size; j++) {
316 x = x + fastrand () % 3 - 1;
317 y = y + fastrand () % 3 - 1;
319 if (y >= height || x >= width) break;
321 dest[y * width + x] = 0xc0c0c0;
327 aging_mode_switch (GstAgingTV *filter)
329 switch (filter->aging_mode) {
332 filter->scratch_lines = 7;
333 /* Most of the parameters are tuned for 640x480 mode */
334 /* area_scale is set to 10 when screen size is 640x480. */
335 filter->area_scale = filter->width * filter->height / 64 / 480;
337 if (filter->area_scale <= 0)
338 filter->area_scale = 1;
342 gst_agingtv_chain (GstPad * pad, GstBuffer * buf)
348 filter = GST_AGINGTV (gst_pad_get_parent (pad));
350 src = (guint32 *) GST_BUFFER_DATA (buf);
352 outbuf = gst_buffer_new ();
353 GST_BUFFER_SIZE (outbuf) = (filter->video_size * sizeof (guint32));
354 dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
355 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
357 coloraging (src, dest, filter->video_size);
358 scratching (filter->scratches, filter->scratch_lines, dest, filter->width, filter->height);
359 pits (dest, filter->width, filter->height, filter->area_scale, filter->pits_interval);
360 if(filter->area_scale > 1)
361 dusts (dest, filter->width, filter->height, filter->dust_interval, filter->area_scale);
363 gst_buffer_unref (buf);
365 gst_pad_push (filter->srcpad, outbuf);
369 gst_agingtv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
373 /* it's not null if we got it, but it might not be ours */
374 g_return_if_fail (GST_IS_AGINGTV (object));
376 filter = GST_AGINGTV (object);
385 gst_agingtv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
389 /* it's not null if we got it, but it might not be ours */
390 g_return_if_fail (GST_IS_AGINGTV (object));
392 filter = GST_AGINGTV (object);
396 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);