Remove unused variables in _class_init
[platform/upstream/gstreamer.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/video/gstvideofilter.h>
41
42 #include <string.h>
43 #include <math.h>
44
45 #include <gst/video/video.h>
46
47 #define GST_TYPE_AGINGTV \
48   (gst_agingtv_get_type())
49 #define GST_AGINGTV(obj) \
50   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_AGINGTV,GstAgingTV))
51 #define GST_AGINGTV_CLASS(klass) \
52   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_AGINGTV,GstAgingTVClass))
53 #define GST_IS_AGINGTV(obj) \
54   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_AGINGTV))
55 #define GST_IS_AGINGTV_CLASS(klass) \
56   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_AGINGTV))
57
58 #define SCRATCH_MAX 20
59 typedef struct _scratch
60 {
61   gint life;
62   gint x;
63   gint dx;
64   gint init;
65 }
66 scratch;
67
68 static int dx[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
69 static int dy[8] = { 0, -1, -1, -1, 0, 1, 1, 1 };
70
71 typedef struct _GstAgingTV GstAgingTV;
72 typedef struct _GstAgingTVClass GstAgingTVClass;
73
74 struct _GstAgingTV
75 {
76   GstVideoFilter videofilter;
77
78   gint width, height;
79   gint aging_mode;
80
81   scratch scratches[SCRATCH_MAX];
82   gint scratch_lines;
83
84   gint dust_interval;
85   gint pits_interval;
86
87 };
88
89 struct _GstAgingTVClass
90 {
91   GstVideoFilterClass parent_class;
92 };
93
94 GType gst_agingtv_get_type (void);
95
96 static const GstElementDetails agingtv_details =
97 GST_ELEMENT_DETAILS ("AgingTV effect",
98     "Filter/Effect/Video",
99     "AgingTV adds age to video input using scratches and dust",
100     "Sam Lantinga <slouken@devolution.com>");
101
102 static GstStaticPadTemplate gst_agingtv_src_template =
103 GST_STATIC_PAD_TEMPLATE ("src",
104     GST_PAD_SRC,
105     GST_PAD_ALWAYS,
106     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
107     );
108
109 static GstStaticPadTemplate gst_agingtv_sink_template =
110 GST_STATIC_PAD_TEMPLATE ("sink",
111     GST_PAD_SINK,
112     GST_PAD_ALWAYS,
113     GST_STATIC_CAPS (GST_VIDEO_CAPS_BGRx)
114     );
115
116 static GstVideoFilterClass *parent_class = NULL;
117
118 static gboolean
119 gst_agingtv_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
120     GstCaps * outcaps)
121 {
122   GstAgingTV *filter = GST_AGINGTV (btrans);
123   GstStructure *structure;
124   gboolean ret = FALSE;
125
126   structure = gst_caps_get_structure (incaps, 0);
127
128   if (gst_structure_get_int (structure, "width", &filter->width) &&
129       gst_structure_get_int (structure, "height", &filter->height)) {
130     ret = TRUE;
131   }
132
133   return ret;
134 }
135
136 static gboolean
137 gst_agingtv_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
138     guint * size)
139 {
140   GstAgingTV *filter;
141   GstStructure *structure;
142   gboolean ret = FALSE;
143   gint width, height;
144
145   filter = GST_AGINGTV (btrans);
146
147   structure = gst_caps_get_structure (caps, 0);
148
149   if (gst_structure_get_int (structure, "width", &width) &&
150       gst_structure_get_int (structure, "height", &height)) {
151     *size = width * height * 32 / 8;
152     ret = TRUE;
153     GST_DEBUG_OBJECT (filter, "our frame size is %d bytes (%dx%d)", *size,
154         width, height);
155   }
156
157   return ret;
158 }
159
160 static unsigned int
161 fastrand (void)
162 {
163   static unsigned int fastrand_val;
164
165   return (fastrand_val = fastrand_val * 1103515245 + 12345);
166 }
167
168
169 static void
170 coloraging (guint32 * src, guint32 * dest, gint video_area)
171 {
172   guint32 a, b;
173   gint i;
174
175   for (i = video_area; i; i--) {
176     a = *src++;
177     b = (a & 0xfcfcfc) >> 2;
178     *dest++ = a - b + 0x181818 + ((fastrand () >> 8) & 0x101010);
179   }
180 }
181
182
183 static void
184 scratching (scratch * scratches, gint scratch_lines, guint32 * dest, gint width,
185     gint height)
186 {
187   gint i, y, y1, y2;
188   guint32 *p, a, b;
189   scratch *scratch;
190
191   for (i = 0; i < scratch_lines; i++) {
192     scratch = &scratches[i];
193
194     if (scratch->life) {
195       scratch->x = scratch->x + scratch->dx;
196
197       if (scratch->x < 0 || scratch->x > width * 256) {
198         scratch->life = 0;
199         break;
200       }
201       p = dest + (scratch->x >> 8);
202       if (scratch->init) {
203         y1 = scratch->init;
204         scratch->init = 0;
205       } else {
206         y1 = 0;
207       }
208       scratch->life--;
209       if (scratch->life) {
210         y2 = height;
211       } else {
212         y2 = fastrand () % height;
213       }
214       for (y = y1; y < y2; y++) {
215         a = *p & 0xfefeff;
216         a += 0x202020;
217         b = a & 0x1010100;
218         *p = a | (b - (b >> 8));
219         p += width;
220       }
221     } else {
222       if ((fastrand () & 0xf0000000) == 0) {
223         scratch->life = 2 + (fastrand () >> 27);
224         scratch->x = fastrand () % (width * 256);
225         scratch->dx = ((int) fastrand ()) >> 23;
226         scratch->init = (fastrand () % (height - 1)) + 1;
227       }
228     }
229   }
230 }
231
232 static void
233 dusts (guint32 * dest, gint width, gint height, gint dust_interval,
234     gint area_scale)
235 {
236   int i, j;
237   int dnum;
238   int d, len;
239   guint x, y;
240
241   if (dust_interval == 0) {
242     if ((fastrand () & 0xf0000000) == 0) {
243       dust_interval = fastrand () >> 29;
244     }
245     return;
246   }
247   dnum = area_scale * 4 + (fastrand () >> 27);
248
249   for (i = 0; i < dnum; i++) {
250     x = fastrand () % width;
251     y = fastrand () % height;
252     d = fastrand () >> 29;
253     len = fastrand () % area_scale + 5;
254     for (j = 0; j < len; j++) {
255       dest[y * width + x] = 0x101010;
256       y += dy[d];
257       x += dx[d];
258
259       if (y >= height || x >= width)
260         break;
261
262       d = (d + fastrand () % 3 - 1) & 7;
263     }
264   }
265   dust_interval--;
266 }
267
268 static void
269 pits (guint32 * dest, gint width, gint height, gint area_scale,
270     gint pits_interval)
271 {
272   int i, j;
273   int pnum, size, pnumscale;
274   guint x, y;
275
276   pnumscale = area_scale * 2;
277   if (pits_interval) {
278     pnum = pnumscale + (fastrand () % pnumscale);
279
280     pits_interval--;
281   } else {
282     pnum = fastrand () % pnumscale;
283
284     if ((fastrand () & 0xf8000000) == 0) {
285       pits_interval = (fastrand () >> 28) + 20;
286     }
287   }
288   for (i = 0; i < pnum; i++) {
289     x = fastrand () % (width - 1);
290     y = fastrand () % (height - 1);
291
292     size = fastrand () >> 28;
293
294     for (j = 0; j < size; j++) {
295       x = x + fastrand () % 3 - 1;
296       y = y + fastrand () % 3 - 1;
297
298       if (y >= height || x >= width)
299         break;
300
301       dest[y * width + x] = 0xc0c0c0;
302     }
303   }
304 }
305
306 static GstFlowReturn
307 gst_agingtv_transform (GstBaseTransform * trans, GstBuffer * in,
308     GstBuffer * out)
309 {
310   GstAgingTV *agingtv = GST_AGINGTV (trans);
311   gint width = agingtv->width;
312   gint height = agingtv->height;
313   int video_size = width * height;
314   guint32 *src = (guint32 *) GST_BUFFER_DATA (in);
315   guint32 *dest = (guint32 *) GST_BUFFER_DATA (out);
316   gint area_scale = width * height / 64 / 480;
317   GstFlowReturn ret = GST_FLOW_OK;
318
319   gst_buffer_copy_metadata (out, in, GST_BUFFER_COPY_TIMESTAMPS);
320
321   if (area_scale <= 0)
322     area_scale = 1;
323
324   coloraging (src, dest, video_size);
325   scratching (agingtv->scratches, agingtv->scratch_lines, dest, width, height);
326   pits (dest, width, height, area_scale, agingtv->pits_interval);
327   if (area_scale > 1)
328     dusts (dest, width, height, agingtv->dust_interval, area_scale);
329
330   return ret;
331 }
332
333 static void
334 gst_agingtv_base_init (gpointer g_class)
335 {
336   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
337
338   gst_element_class_set_details (element_class, &agingtv_details);
339
340   gst_element_class_add_pad_template (element_class,
341       gst_static_pad_template_get (&gst_agingtv_sink_template));
342   gst_element_class_add_pad_template (element_class,
343       gst_static_pad_template_get (&gst_agingtv_src_template));
344 }
345
346 static void
347 gst_agingtv_class_init (gpointer klass, gpointer class_data)
348 {
349   GstBaseTransformClass *trans_class;
350
351   trans_class = (GstBaseTransformClass *) klass;
352
353   parent_class = g_type_class_peek_parent (klass);
354
355   trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_agingtv_set_caps);
356   trans_class->get_unit_size = GST_DEBUG_FUNCPTR (gst_agingtv_get_unit_size);
357   trans_class->transform = GST_DEBUG_FUNCPTR (gst_agingtv_transform);
358 }
359
360 static void
361 gst_agingtv_init (GTypeInstance * instance, gpointer g_class)
362 {
363 }
364
365 GType
366 gst_agingtv_get_type (void)
367 {
368   static GType agingtv_type = 0;
369
370   if (!agingtv_type) {
371     static const GTypeInfo agingtv_info = {
372       sizeof (GstAgingTVClass),
373       gst_agingtv_base_init,
374       NULL,
375       gst_agingtv_class_init,
376       NULL,
377       NULL,
378       sizeof (GstAgingTV),
379       0,
380       gst_agingtv_init,
381     };
382
383     agingtv_type = g_type_register_static (GST_TYPE_VIDEO_FILTER,
384         "GstAgingTV", &agingtv_info, 0);
385   }
386   return agingtv_type;
387 }