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.
28 #include "gsteffectv.h"
30 #define GST_TYPE_AGINGTV \
31 (gst_agingtv_get_type())
32 #define GST_AGINGTV(obj) \
33 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AGINGTV,GstAgingTV))
34 #define GST_AGINGTV_CLASS(klass) \
35 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstAgingTV))
36 #define GST_IS_AGINGTV(obj) \
37 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AGINGTV))
38 #define GST_IS_AGINGTV_CLASS(obj) \
39 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AGINGTV))
41 #define SCRATCH_MAX 20
42 typedef struct _scratch
50 static int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1};
51 static int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1};
54 typedef struct _GstAgingTV GstAgingTV;
55 typedef struct _GstAgingTVClass GstAgingTVClass;
61 GstPad *sinkpad, *srcpad;
68 scratch scratches[SCRATCH_MAX];
75 struct _GstAgingTVClass
77 GstElementClass parent_class;
80 /* elementfactory information */
81 GstElementDetails gst_agingtv_details = {
83 "Filter/Video/Effect",
85 "Aply aging effect on video",
87 "Wim Taymans <wim.taymans@chello.be>",
88 "(C) 2001 FUKUCHI Kentarou",
92 /* Filter signals and args */
104 static void gst_agingtv_class_init (GstAgingTVClass * klass);
105 static void gst_agingtv_init (GstAgingTV * filter);
107 static void aging_mode_switch (GstAgingTV *filter);
109 static void gst_agingtv_set_property (GObject * object, guint prop_id,
110 const GValue * value, GParamSpec * pspec);
111 static void gst_agingtv_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec);
114 static void gst_agingtv_chain (GstPad * pad, GstData *_data);
116 static GstElementClass *parent_class = NULL;
117 /*static guint gst_agingtv_signals[LAST_SIGNAL] = { 0 }; */
119 GType gst_agingtv_get_type (void)
121 static GType agingtv_type = 0;
124 static const GTypeInfo agingtv_info = {
125 sizeof (GstAgingTVClass), NULL,
127 (GClassInitFunc) gst_agingtv_class_init,
132 (GInstanceInitFunc) gst_agingtv_init,
135 agingtv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstAgingTV", &agingtv_info, 0);
141 gst_agingtv_class_init (GstAgingTVClass * klass)
143 GObjectClass *gobject_class;
144 GstElementClass *gstelement_class;
146 gobject_class = (GObjectClass *) klass;
147 gstelement_class = (GstElementClass *) klass;
149 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
151 gobject_class->set_property = gst_agingtv_set_property;
152 gobject_class->get_property = gst_agingtv_get_property;
155 static GstPadLinkReturn
156 gst_agingtv_sinkconnect (GstPad * pad, GstCaps * caps)
160 filter = GST_AGINGTV (gst_pad_get_parent (pad));
162 if (!GST_CAPS_IS_FIXED (caps))
163 return GST_PAD_LINK_DELAYED;
165 gst_caps_get_int (caps, "width", &filter->width);
166 gst_caps_get_int (caps, "height", &filter->height);
168 filter->video_size = filter->width * filter->height;
169 filter->aging_mode = 0;
170 aging_mode_switch (filter);
172 return gst_pad_try_set_caps (filter->srcpad, gst_caps_ref (caps));
176 gst_agingtv_init (GstAgingTV * filter)
178 filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
179 gst_pad_set_chain_function (filter->sinkpad, gst_agingtv_chain);
180 gst_pad_set_link_function (filter->sinkpad, gst_agingtv_sinkconnect);
181 gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
183 filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
184 gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
191 static unsigned int fastrand_val;
193 return (fastrand_val = fastrand_val * 1103515245 + 12345);
198 coloraging (guint32 *src, guint32 *dest, gint video_area)
203 for (i = video_area; i; i--) {
205 b = (a & 0xfcfcfc) >> 2;
206 *dest++ = a - b + 0x181818 + ((fastrand () >> 8) & 0x101010);
212 scratching (scratch *scratches, gint scratch_lines, guint32 *dest, gint width, gint height)
218 for (i = 0; i < scratch_lines; i++) {
219 scratch = &scratches[i];
222 scratch->x = scratch->x + scratch->dx;
224 if (scratch->x < 0 || scratch->x > width * 256) {
228 p = dest + (scratch->x >> 8);
239 y2 = fastrand () % height;
241 for (y = y1; y < y2; y++) {
245 *p = a | (b - (b >> 8));
249 if ((fastrand () & 0xf0000000) == 0) {
250 scratch->life = 2 + (fastrand () >> 27);
251 scratch->x = fastrand () % (width * 256);
252 scratch->dx = ((int) fastrand ()) >> 23;
253 scratch->init = (fastrand () % (height - 1)) + 1;
260 dusts (guint32 *dest, gint width, gint height, gint dust_interval, gint area_scale)
267 if (dust_interval == 0) {
268 if ((fastrand () & 0xf0000000) == 0) {
269 dust_interval = fastrand () >> 29;
273 dnum = area_scale * 4 + (fastrand() >> 27);
275 for (i = 0; i < dnum; i++) {
276 x = fastrand () % width;
277 y = fastrand () % height;
278 d = fastrand () >> 29;
279 len = fastrand () % area_scale + 5;
280 for (j = 0; j < len; j++) {
281 dest[y * width + x] = 0x101010;
285 if (y >= height || x >= width) break;
287 d = (d + fastrand () % 3 - 1) & 7;
294 pits (guint32 *dest, gint width, gint height, gint area_scale, gint pits_interval)
297 int pnum, size, pnumscale;
300 pnumscale = area_scale * 2;
302 pnum = pnumscale + (fastrand () % pnumscale);
306 pnum = fastrand () % pnumscale;
308 if ((fastrand () & 0xf8000000) == 0) {
309 pits_interval = (fastrand () >> 28) + 20;
312 for (i = 0; i < pnum; i++) {
313 x = fastrand () % (width - 1);
314 y = fastrand () % (height - 1);
316 size = fastrand () >> 28;
318 for (j = 0; j < size; j++) {
319 x = x + fastrand () % 3 - 1;
320 y = y + fastrand () % 3 - 1;
322 if (y >= height || x >= width) break;
324 dest[y * width + x] = 0xc0c0c0;
330 aging_mode_switch (GstAgingTV *filter)
332 switch (filter->aging_mode) {
335 filter->scratch_lines = 7;
336 /* Most of the parameters are tuned for 640x480 mode */
337 /* area_scale is set to 10 when screen size is 640x480. */
338 filter->area_scale = filter->width * filter->height / 64 / 480;
340 if (filter->area_scale <= 0)
341 filter->area_scale = 1;
345 gst_agingtv_chain (GstPad * pad, GstData *_data)
347 GstBuffer *buf = GST_BUFFER (_data);
352 filter = GST_AGINGTV (gst_pad_get_parent (pad));
354 src = (guint32 *) GST_BUFFER_DATA (buf);
356 outbuf = gst_buffer_new ();
357 GST_BUFFER_SIZE (outbuf) = (filter->video_size * sizeof (guint32));
358 dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
359 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
361 coloraging (src, dest, filter->video_size);
362 scratching (filter->scratches, filter->scratch_lines, dest, filter->width, filter->height);
363 pits (dest, filter->width, filter->height, filter->area_scale, filter->pits_interval);
364 if(filter->area_scale > 1)
365 dusts (dest, filter->width, filter->height, filter->dust_interval, filter->area_scale);
367 gst_buffer_unref (buf);
369 gst_pad_push (filter->srcpad, GST_DATA (outbuf));
373 gst_agingtv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
377 /* it's not null if we got it, but it might not be ours */
378 g_return_if_fail (GST_IS_AGINGTV (object));
380 filter = GST_AGINGTV (object);
389 gst_agingtv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
393 /* it's not null if we got it, but it might not be ours */
394 g_return_if_fail (GST_IS_AGINGTV (object));
396 filter = GST_AGINGTV (object);
400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);