gst-indent
[platform/upstream/gst-plugins-good.git] / gst / effectv / gstaging.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
4  *
5  * EffecTV - Realtime Digital Video Effector
6  * Copyright (C) 2001 FUKUCHI Kentarou
7  *
8  * This library is free software; 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.
12  *
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.
17  *
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.
22  */
23
24 /*
25  * This file was (probably) generated from gstvideotemplate.c,
26  * gstvideotemplate.c,v 1.11 2004/01/07 08:56:45 ds Exp 
27  */
28
29 /* From main.c of warp-1.1:
30  *
31  *      Simple DirectMedia Layer demo
32  *      Realtime picture 'gooing'
33  *      by sam lantinga slouken@devolution.com
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <gst/gst.h>
41 #include <gstvideofilter.h>
42 #include <string.h>
43 #include <math.h>
44
45 #define GST_TYPE_AGINGTV \
46   (gst_agingtv_get_type())
47 #define GST_AGINGTV(obj) \
48   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AGINGTV,GstAgingTV))
49 #define GST_AGINGTV_CLASS(klass) \
50   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AGINGTV,GstAgingTVClass))
51 #define GST_IS_AGINGTV(obj) \
52   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AGINGTV))
53 #define GST_IS_AGINGTV_CLASS(obj) \
54   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AGINGTV))
55
56 #define SCRATCH_MAX 20
57 typedef struct _scratch
58 {
59   gint life;
60   gint x;
61   gint dx;
62   gint init;
63 } scratch;
64
65 static int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
66 static int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
67
68 typedef struct _GstAgingTV GstAgingTV;
69 typedef struct _GstAgingTVClass GstAgingTVClass;
70
71 struct _GstAgingTV
72 {
73   GstVideofilter videofilter;
74
75   gint width, height;
76   gint aging_mode;
77
78   scratch scratches[SCRATCH_MAX];
79   gint scratch_lines;
80
81   gint dust_interval;
82   gint pits_interval;
83
84 };
85
86 struct _GstAgingTVClass
87 {
88   GstVideofilterClass parent_class;
89 };
90
91 /* GstAgingTV signals and args */
92 enum
93 {
94   /* FILL ME */
95   LAST_SIGNAL
96 };
97
98 enum
99 {
100   ARG_0,
101   /* FILL ME */
102 };
103
104 static void gst_agingtv_base_init (gpointer g_class);
105 static void gst_agingtv_class_init (gpointer g_class, gpointer class_data);
106 static void gst_agingtv_init (GTypeInstance * instance, gpointer g_class);
107 static void gst_agingtv_setup (GstVideofilter * videofilter);
108
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);
113 static void gst_agingtv_rgb32 (GstVideofilter * videofilter, void *d, void *s);
114
115 GType
116 gst_agingtv_get_type (void)
117 {
118   static GType agingtv_type = 0;
119
120   if (!agingtv_type) {
121     static const GTypeInfo agingtv_info = {
122       sizeof (GstAgingTVClass),
123       gst_agingtv_base_init,
124       NULL,
125       gst_agingtv_class_init,
126       NULL,
127       NULL,
128       sizeof (GstAgingTV),
129       0,
130       gst_agingtv_init,
131     };
132     agingtv_type = g_type_register_static (GST_TYPE_VIDEOFILTER,
133         "GstAgingTV", &agingtv_info, 0);
134   }
135   return agingtv_type;
136 }
137
138 static GstVideofilterFormat gst_agingtv_formats[] = {
139   {"RGB ", 32, gst_agingtv_rgb32, 24, G_BIG_ENDIAN, 0x0000ff00, 0x00ff0000,
140       0xff000000}
141 };
142
143 static void
144 gst_agingtv_base_init (gpointer g_class)
145 {
146   static GstElementDetails agingtv_details = GST_ELEMENT_DETAILS ("AgingTV",
147       "Filter/Effect/Video",
148       "AgingTV adds age to video input using scratches and dust",
149       "Sam Lantinga <slouken@devolution.com>");
150   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
151   GstVideofilterClass *videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
152   int i;
153
154   gst_element_class_set_details (element_class, &agingtv_details);
155
156   for (i = 0; i < G_N_ELEMENTS (gst_agingtv_formats); i++) {
157     gst_videofilter_class_add_format (videofilter_class,
158         gst_agingtv_formats + i);
159   }
160
161   gst_videofilter_class_add_pad_templates (GST_VIDEOFILTER_CLASS (g_class));
162 }
163
164 static void
165 gst_agingtv_class_init (gpointer g_class, gpointer class_data)
166 {
167   GObjectClass *gobject_class;
168   GstVideofilterClass *videofilter_class;
169
170   gobject_class = G_OBJECT_CLASS (g_class);
171   videofilter_class = GST_VIDEOFILTER_CLASS (g_class);
172
173 #if 0
174   g_object_class_install_property (gobject_class, ARG_METHOD,
175       g_param_spec_enum ("method", "method", "method",
176           GST_TYPE_AGINGTV_METHOD, GST_AGINGTV_METHOD_1, G_PARAM_READWRITE));
177 #endif
178
179   gobject_class->set_property = gst_agingtv_set_property;
180   gobject_class->get_property = gst_agingtv_get_property;
181
182   videofilter_class->setup = gst_agingtv_setup;
183 }
184
185 static void
186 gst_agingtv_init (GTypeInstance * instance, gpointer g_class)
187 {
188   GstAgingTV *agingtv = GST_AGINGTV (instance);
189   GstVideofilter *videofilter;
190
191   GST_DEBUG ("gst_agingtv_init");
192
193   videofilter = GST_VIDEOFILTER (agingtv);
194
195   /* do stuff */
196 }
197
198 static void
199 gst_agingtv_setup (GstVideofilter * videofilter)
200 {
201   GstAgingTV *agingtv;
202   int width = gst_videofilter_get_input_width (videofilter);
203   int height = gst_videofilter_get_input_height (videofilter);
204
205   g_return_if_fail (GST_IS_AGINGTV (videofilter));
206   agingtv = GST_AGINGTV (videofilter);
207
208   /* if any setup needs to be done, do it here */
209
210   agingtv->width = width;
211   agingtv->height = height;
212 }
213
214 static unsigned int
215 fastrand (void)
216 {
217   static unsigned int fastrand_val;
218
219   return (fastrand_val = fastrand_val * 1103515245 + 12345);
220 }
221
222
223 static void
224 coloraging (guint32 * src, guint32 * dest, gint video_area)
225 {
226   guint32 a, b;
227   gint i;
228
229   for (i = video_area; i; i--) {
230     a = *src++;
231     b = (a & 0xfcfcfc) >> 2;
232     *dest++ = a - b + 0x181818 + ((fastrand () >> 8) & 0x101010);
233   }
234 }
235
236
237 static void
238 scratching (scratch * scratches, gint scratch_lines, guint32 * dest, gint width,
239     gint height)
240 {
241   gint i, y, y1, y2;
242   guint32 *p, a, b;
243   scratch *scratch;
244
245   for (i = 0; i < scratch_lines; i++) {
246     scratch = &scratches[i];
247
248     if (scratch->life) {
249       scratch->x = scratch->x + scratch->dx;
250
251       if (scratch->x < 0 || scratch->x > width * 256) {
252         scratch->life = 0;
253         break;
254       }
255       p = dest + (scratch->x >> 8);
256       if (scratch->init) {
257         y1 = scratch->init;
258         scratch->init = 0;
259       } else {
260         y1 = 0;
261       }
262       scratch->life--;
263       if (scratch->life) {
264         y2 = height;
265       } else {
266         y2 = fastrand () % height;
267       }
268       for (y = y1; y < y2; y++) {
269         a = *p & 0xfefeff;
270         a += 0x202020;
271         b = a & 0x1010100;
272         *p = a | (b - (b >> 8));
273         p += width;
274       }
275     } else {
276       if ((fastrand () & 0xf0000000) == 0) {
277         scratch->life = 2 + (fastrand () >> 27);
278         scratch->x = fastrand () % (width * 256);
279         scratch->dx = ((int) fastrand ()) >> 23;
280         scratch->init = (fastrand () % (height - 1)) + 1;
281       }
282     }
283   }
284 }
285
286 static void
287 dusts (guint32 * dest, gint width, gint height, gint dust_interval,
288     gint area_scale)
289 {
290   int i, j;
291   int dnum;
292   int d, len;
293   guint x, y;
294
295   if (dust_interval == 0) {
296     if ((fastrand () & 0xf0000000) == 0) {
297       dust_interval = fastrand () >> 29;
298     }
299     return;
300   }
301   dnum = area_scale * 4 + (fastrand () >> 27);
302
303   for (i = 0; i < dnum; i++) {
304     x = fastrand () % width;
305     y = fastrand () % height;
306     d = fastrand () >> 29;
307     len = fastrand () % area_scale + 5;
308     for (j = 0; j < len; j++) {
309       dest[y * width + x] = 0x101010;
310       y += dy[d];
311       x += dx[d];
312
313       if (y >= height || x >= width)
314         break;
315
316       d = (d + fastrand () % 3 - 1) & 7;
317     }
318   }
319   dust_interval--;
320 }
321
322 static void
323 pits (guint32 * dest, gint width, gint height, gint area_scale,
324     gint pits_interval)
325 {
326   int i, j;
327   int pnum, size, pnumscale;
328   guint x, y;
329
330   pnumscale = area_scale * 2;
331   if (pits_interval) {
332     pnum = pnumscale + (fastrand () % pnumscale);
333
334     pits_interval--;
335   } else {
336     pnum = fastrand () % pnumscale;
337
338     if ((fastrand () & 0xf8000000) == 0) {
339       pits_interval = (fastrand () >> 28) + 20;
340     }
341   }
342   for (i = 0; i < pnum; i++) {
343     x = fastrand () % (width - 1);
344     y = fastrand () % (height - 1);
345
346     size = fastrand () >> 28;
347
348     for (j = 0; j < size; j++) {
349       x = x + fastrand () % 3 - 1;
350       y = y + fastrand () % 3 - 1;
351
352       if (y >= height || x >= width)
353         break;
354
355       dest[y * width + x] = 0xc0c0c0;
356     }
357   }
358 }
359
360 static void
361 gst_agingtv_rgb32 (GstVideofilter * videofilter, void *d, void *s)
362 {
363   GstAgingTV *agingtv;
364   int width = gst_videofilter_get_input_width (videofilter);
365   int height = gst_videofilter_get_input_height (videofilter);
366   int video_size = width * height;
367   guint32 *src = s;
368   guint32 *dest = d;
369   gint area_scale = width * height / 64 / 480;
370
371   if (area_scale <= 0)
372     area_scale = 1;
373
374   g_return_if_fail (GST_IS_AGINGTV (videofilter));
375   agingtv = GST_AGINGTV (videofilter);
376
377   coloraging (src, dest, video_size);
378   scratching (agingtv->scratches, agingtv->scratch_lines, dest, width, height);
379   pits (dest, width, height, area_scale, agingtv->pits_interval);
380   if (area_scale > 1)
381     dusts (dest, width, height, agingtv->dust_interval, area_scale);
382
383 }
384
385 static void
386 gst_agingtv_set_property (GObject * object, guint prop_id, const GValue * value,
387     GParamSpec * pspec)
388 {
389   GstAgingTV *src;
390
391   /* it's not null if we got it, but it might not be ours */
392   g_return_if_fail (GST_IS_AGINGTV (object));
393   src = GST_AGINGTV (object);
394
395   GST_DEBUG ("gst_agingtv_set_property");
396   switch (prop_id) {
397 #if 0
398     case ARG_METHOD:
399       src->method = g_value_get_enum (value);
400       break;
401 #endif
402     default:
403       break;
404   }
405 }
406
407 static void
408 gst_agingtv_get_property (GObject * object, guint prop_id, GValue * value,
409     GParamSpec * pspec)
410 {
411   GstAgingTV *src;
412
413   /* it's not null if we got it, but it might not be ours */
414   g_return_if_fail (GST_IS_AGINGTV (object));
415   src = GST_AGINGTV (object);
416
417   switch (prop_id) {
418 #if 0
419     case ARG_METHOD:
420       g_value_set_enum (value, src->method);
421       break;
422 #endif
423     default:
424       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
425       break;
426   }
427 }