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 GstElementDetails gst_agingtv_details = {
79 "Filter/Video/Effect",
80 "Aply aging effect on video",
82 "Wim Taymans <wim.taymans@chello.be>",
83 "(C) 2001 FUKUCHI Kentarou",
87 /* Filter signals and args */
99 static void gst_agingtv_class_init (GstAgingTVClass * klass);
100 static void gst_agingtv_init (GstAgingTV * filter);
102 static void aging_mode_switch (GstAgingTV *filter);
104 static void gst_agingtv_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_agingtv_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
109 static void gst_agingtv_chain (GstPad * pad, GstBuffer * buf);
111 static GstElementClass *parent_class = NULL;
112 /*static guint gst_agingtv_signals[LAST_SIGNAL] = { 0 }; */
114 GType gst_agingtv_get_type (void)
116 static GType agingtv_type = 0;
119 static const GTypeInfo agingtv_info = {
120 sizeof (GstAgingTVClass), NULL,
122 (GClassInitFunc) gst_agingtv_class_init,
127 (GInstanceInitFunc) gst_agingtv_init,
130 agingtv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAgingTV", &agingtv_info, 0);
136 gst_agingtv_class_init (GstAgingTVClass * klass)
138 GObjectClass *gobject_class;
139 GstElementClass *gstelement_class;
141 gobject_class = (GObjectClass *) klass;
142 gstelement_class = (GstElementClass *) klass;
144 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
146 gobject_class->set_property = gst_agingtv_set_property;
147 gobject_class->get_property = gst_agingtv_get_property;
150 static GstPadConnectReturn
151 gst_agingtv_sinkconnect (GstPad * pad, GstCaps * caps)
155 filter = GST_AGINGTV (gst_pad_get_parent (pad));
157 if (!GST_CAPS_IS_FIXED (caps))
158 return GST_PAD_CONNECT_DELAYED;
160 gst_caps_get_int (caps, "width", &filter->width);
161 gst_caps_get_int (caps, "height", &filter->height);
163 filter->video_size = filter->width * filter->height;
164 filter->aging_mode = 0;
165 aging_mode_switch (filter);
167 return gst_pad_try_set_caps (filter->srcpad, caps);
171 gst_agingtv_init (GstAgingTV * filter)
173 filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
174 gst_pad_set_chain_function (filter->sinkpad, gst_agingtv_chain);
175 gst_pad_set_connect_function (filter->sinkpad, gst_agingtv_sinkconnect);
176 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
178 filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
179 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
186 static unsigned int fastrand_val;
188 return (fastrand_val = fastrand_val * 1103515245 + 12345);
193 coloraging (guint32 *src, guint32 *dest, gint video_area)
198 for (i = video_area; i; i--) {
200 b = (a & 0xfcfcfc) >> 2;
201 *dest++ = a - b + 0x181818 + ((fastrand () >> 8) & 0x101010);
207 scratching (scratch *scratches, gint scratch_lines, guint32 *dest, gint width, gint height)
213 for (i = 0; i < scratch_lines; i++) {
214 scratch = &scratches[i];
217 scratch->x = scratch->x + scratch->dx;
219 if (scratch->x < 0 || scratch->x > width * 256) {
223 p = dest + (scratch->x >> 8);
234 y2 = fastrand () % height;
236 for (y = y1; y < y2; y++) {
240 *p = a | (b - (b >> 8));
244 if ((fastrand () & 0xf0000000) == 0) {
245 scratch->life = 2 + (fastrand () >> 27);
246 scratch->x = fastrand () % (width * 256);
247 scratch->dx = ((int) fastrand ()) >> 23;
248 scratch->init = (fastrand () % (height - 1)) + 1;
255 dusts (guint32 *dest, gint width, gint height, gint dust_interval, gint area_scale)
262 if (dust_interval == 0) {
263 if ((fastrand () & 0xf0000000) == 0) {
264 dust_interval = fastrand () >> 29;
268 dnum = area_scale * 4 + (fastrand() >> 27);
270 for (i = 0; i < dnum; i++) {
271 x = fastrand () % width;
272 y = fastrand () % height;
273 d = fastrand () >> 29;
274 len = fastrand () % area_scale + 5;
275 for (j = 0; j < len; j++) {
276 dest[y * width + x] = 0x101010;
280 if (y >= height || x >= width) break;
282 d = (d + fastrand () % 3 - 1) & 7;
289 pits (guint32 *dest, gint width, gint height, gint area_scale, gint pits_interval)
292 int pnum, size, pnumscale;
295 pnumscale = area_scale * 2;
297 pnum = pnumscale + (fastrand () % pnumscale);
301 pnum = fastrand () % pnumscale;
303 if ((fastrand () & 0xf8000000) == 0) {
304 pits_interval = (fastrand () >> 28) + 20;
307 for (i = 0; i < pnum; i++) {
308 x = fastrand () % (width - 1);
309 y = fastrand () % (height - 1);
311 size = fastrand () >> 28;
313 for (j = 0; j < size; j++) {
314 x = x + fastrand () % 3 - 1;
315 y = y + fastrand () % 3 - 1;
317 if (y >= height || x >= width) break;
319 dest[y * width + x] = 0xc0c0c0;
325 aging_mode_switch (GstAgingTV *filter)
327 switch (filter->aging_mode) {
330 filter->scratch_lines = 7;
331 /* Most of the parameters are tuned for 640x480 mode */
332 /* area_scale is set to 10 when screen size is 640x480. */
333 filter->area_scale = filter->width * filter->height / 64 / 480;
335 if (filter->area_scale <= 0)
336 filter->area_scale = 1;
340 gst_agingtv_chain (GstPad * pad, GstBuffer * buf)
346 filter = GST_AGINGTV (gst_pad_get_parent (pad));
348 src = (guint32 *) GST_BUFFER_DATA (buf);
350 outbuf = gst_buffer_new ();
351 GST_BUFFER_SIZE (outbuf) = (filter->video_size * sizeof (guint32));
352 dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
353 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
355 coloraging (src, dest, filter->video_size);
356 scratching (filter->scratches, filter->scratch_lines, dest, filter->width, filter->height);
357 pits (dest, filter->width, filter->height, filter->area_scale, filter->pits_interval);
358 if(filter->area_scale > 1)
359 dusts (dest, filter->width, filter->height, filter->dust_interval, filter->area_scale);
361 gst_buffer_unref (buf);
363 gst_pad_push (filter->srcpad, outbuf);
367 gst_agingtv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
371 /* it's not null if we got it, but it might not be ours */
372 g_return_if_fail (GST_IS_AGINGTV (object));
374 filter = GST_AGINGTV (object);
383 gst_agingtv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
387 /* it's not null if we got it, but it might not be ours */
388 g_return_if_fail (GST_IS_AGINGTV (object));
390 filter = GST_AGINGTV (object);
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);