48641e6eaa2bd700739eb4b728c4e1a30265b875
[platform/upstream/gstreamer.git] / gst / effectv / gstquark.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * EffecTV:
5  * Copyright (C) 2001 FUKUCHI Kentarou
6  *
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.
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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include <math.h>
28 #include <string.h>
29 #include <gst/gst.h>
30 #include "gsteffectv.h"
31
32 #define GST_TYPE_QUARKTV \
33   (gst_quarktv_get_type())
34 #define GST_QUARKTV(obj) \
35   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_QUARKTV,GstQuarkTV))
36 #define GST_QUARKTV_CLASS(klass) \
37   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_QUARKTV,GstQuarkTVClass))
38 #define GST_IS_QUARKTV(obj) \
39   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_QUARKTV))
40 #define GST_IS_QUARKTV_CLASS(obj) \
41   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_QUARKTV))
42
43 /* number of frames of time-buffer. It should be as a configurable paramter */
44 /* This number also must be 2^n just for the speed. */
45 #define PLANES 16
46
47 typedef struct _GstQuarkTV GstQuarkTV;
48 typedef struct _GstQuarkTVClass GstQuarkTVClass;
49
50 struct _GstQuarkTV
51 {
52   GstElement element;
53
54   GstPad *sinkpad, *srcpad;
55
56   gint width, height;
57   gint area;
58   gint planes;
59   gint current_plane;
60   GstBuffer **planetable;
61 };
62
63 struct _GstQuarkTVClass
64 {
65   GstElementClass parent_class;
66 };
67
68 /* elementfactory information */
69 static GstElementDetails gst_quarktv_details = GST_ELEMENT_DETAILS ("QuarkTV",
70     "Filter/Effect/Video",
71     "Motion dissolver",
72     "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>");
73
74 /* Filter signals and args */
75 enum
76 {
77   /* FILL ME */
78   LAST_SIGNAL
79 };
80
81 enum
82 {
83   ARG_0,
84   ARG_PLANES
85 };
86
87 static void gst_quarktv_base_init (gpointer g_class);
88 static void gst_quarktv_class_init (GstQuarkTVClass * klass);
89 static void gst_quarktv_init (GstQuarkTV * filter);
90
91 static GstStateChangeReturn gst_quarktv_change_state (GstElement * element,
92     GstStateChange transition);
93
94 static void gst_quarktv_set_property (GObject * object, guint prop_id,
95     const GValue * value, GParamSpec * pspec);
96 static void gst_quarktv_get_property (GObject * object, guint prop_id,
97     GValue * value, GParamSpec * pspec);
98
99 static GstFlowReturn gst_quarktv_chain (GstPad * pad, GstBuffer * buffer);
100
101 static GstElementClass *parent_class = NULL;
102
103 /* static guint gst_quarktv_signals[LAST_SIGNAL] = { 0 }; */
104
105 static inline guint32
106 fastrand (void)
107 {
108   static unsigned int fastrand_val;
109
110   return (fastrand_val = fastrand_val * 1103515245 + 12345);
111 }
112
113 GType
114 gst_quarktv_get_type (void)
115 {
116   static GType quarktv_type = 0;
117
118   if (!quarktv_type) {
119     static const GTypeInfo quarktv_info = {
120       sizeof (GstQuarkTVClass),
121       gst_quarktv_base_init,
122       NULL,
123       (GClassInitFunc) gst_quarktv_class_init,
124       NULL,
125       NULL,
126       sizeof (GstQuarkTV),
127       0,
128       (GInstanceInitFunc) gst_quarktv_init,
129     };
130
131     quarktv_type =
132         g_type_register_static (GST_TYPE_ELEMENT, "GstQuarkTV", &quarktv_info,
133         0);
134   }
135   return quarktv_type;
136 }
137
138 static void
139 gst_quarktv_base_init (gpointer g_class)
140 {
141   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
142
143   gst_element_class_add_pad_template (element_class,
144       gst_static_pad_template_get (&gst_effectv_src_template));
145   gst_element_class_add_pad_template (element_class,
146       gst_static_pad_template_get (&gst_effectv_sink_template));
147
148   gst_element_class_set_details (element_class, &gst_quarktv_details);
149 }
150
151 static void
152 gst_quarktv_class_init (GstQuarkTVClass * klass)
153 {
154   GObjectClass *gobject_class;
155   GstElementClass *gstelement_class;
156
157   gobject_class = (GObjectClass *) klass;
158   gstelement_class = (GstElementClass *) klass;
159
160   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
161
162   gobject_class->set_property = gst_quarktv_set_property;
163   gobject_class->get_property = gst_quarktv_get_property;
164
165   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PLANES,
166       g_param_spec_int ("planes", "Planes", "Number of frames in the buffer",
167           1, 32, PLANES, G_PARAM_READWRITE));
168
169   gstelement_class->change_state = gst_quarktv_change_state;
170 }
171
172 static GstPadLinkReturn
173 gst_quarktv_link (GstPad * pad, GstPad * peer)
174 {
175   GstQuarkTV *filter;
176   GstPad *otherpad;
177
178   //gint i;
179   //GstStructure *structure;
180   //GstPadLinkReturn res;
181
182   filter = GST_QUARKTV (gst_pad_get_parent (pad));
183   g_return_val_if_fail (GST_IS_QUARKTV (filter), GST_PAD_LINK_REFUSED);
184
185   otherpad = (pad == filter->srcpad ? filter->sinkpad : filter->srcpad);
186
187 #if 0
188   res = gst_pad_try_set_caps (otherpad, caps);
189   if (GST_PAD_LINK_FAILED (res))
190     return res;
191
192   structure = gst_caps_get_structure (caps, 0);
193   if (!gst_structure_get_int (structure, "width", &filter->width) ||
194       !gst_structure_get_int (structure, "height", &filter->height))
195     return GST_PAD_LINK_REFUSED;
196
197   filter->area = filter->width * filter->height;
198
199   for (i = 0; i < filter->planes; i++) {
200     if (filter->planetable[i])
201       gst_buffer_unref (filter->planetable[i]);
202     filter->planetable[i] = NULL;
203   }
204 #endif
205
206   return GST_PAD_LINK_OK;
207 }
208
209 static void
210 gst_quarktv_init (GstQuarkTV * filter)
211 {
212   filter->sinkpad =
213       gst_pad_new_from_template (gst_static_pad_template_get
214       (&gst_effectv_sink_template), "sink");
215   //gst_pad_set_getcaps_function (filter->sinkpad, gst_pad_proxy_getcaps);
216   gst_pad_set_chain_function (filter->sinkpad, gst_quarktv_chain);
217   gst_pad_set_link_function (filter->sinkpad, gst_quarktv_link);
218   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
219
220   filter->srcpad =
221       gst_pad_new_from_template (gst_static_pad_template_get
222       (&gst_effectv_src_template), "src");
223   //gst_pad_set_getcaps_function (filter->srcpad, gst_pad_proxy_getcaps);
224   gst_pad_set_link_function (filter->srcpad, gst_quarktv_link);
225   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
226
227   filter->planes = PLANES;
228   filter->current_plane = filter->planes - 1;
229 }
230
231 static GstFlowReturn
232 gst_quarktv_chain (GstPad * pad, GstBuffer * buf)
233 {
234   GstQuarkTV *filter;
235   guint32 *src, *dest;
236   GstBuffer *outbuf;
237   gint area;
238   GstFlowReturn ret;
239
240   filter = GST_QUARKTV (gst_pad_get_parent (pad));
241
242   src = (guint32 *) GST_BUFFER_DATA (buf);
243
244   area = filter->area;
245
246   ret =
247       gst_pad_alloc_buffer (filter->srcpad, 0, area, GST_PAD_CAPS (pad),
248       &outbuf);
249   if (ret != GST_FLOW_OK)
250     goto no_buffer;
251
252   dest = (guint32 *) GST_BUFFER_DATA (outbuf);
253   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
254
255   if (filter->planetable[filter->current_plane])
256     gst_buffer_unref (filter->planetable[filter->current_plane]);
257
258   filter->planetable[filter->current_plane] = buf;
259
260   while (--area) {
261     GstBuffer *rand;
262
263     /* pick a random buffer */
264     rand =
265         filter->planetable[(filter->current_plane +
266             (fastrand () >> 24)) & (filter->planes - 1)];
267
268     dest[area] = (rand ? ((guint32 *) GST_BUFFER_DATA (rand))[area] : 0);
269   }
270
271   ret = gst_pad_push (filter->srcpad, outbuf);
272
273   filter->current_plane--;
274   if (filter->current_plane < 0)
275     filter->current_plane = filter->planes - 1;
276
277   return ret;
278
279 no_buffer:
280   {
281     return ret;
282   }
283 }
284
285 static GstStateChangeReturn
286 gst_quarktv_change_state (GstElement * element, GstStateChange transition)
287 {
288   GstQuarkTV *filter = GST_QUARKTV (element);
289
290   switch (transition) {
291     case GST_STATE_CHANGE_PAUSED_TO_READY:
292     {
293       gint i;
294
295       for (i = 0; i < filter->planes; i++) {
296         if (filter->planetable[i])
297           gst_buffer_unref (filter->planetable[i]);
298         filter->planetable[i] = NULL;
299       }
300       g_free (filter->planetable);
301       filter->planetable = NULL;
302       break;
303     }
304     case GST_STATE_CHANGE_READY_TO_PAUSED:
305     {
306       filter->planetable =
307           (GstBuffer **) g_malloc (filter->planes * sizeof (GstBuffer *));
308       memset (filter->planetable, 0, filter->planes * sizeof (GstBuffer *));
309       break;
310     }
311     default:
312       break;
313   }
314
315   return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
316 }
317
318
319 static void
320 gst_quarktv_set_property (GObject * object, guint prop_id, const GValue * value,
321     GParamSpec * pspec)
322 {
323   GstQuarkTV *filter;
324
325   g_return_if_fail (GST_IS_QUARKTV (object));
326
327   filter = GST_QUARKTV (object);
328
329   switch (prop_id) {
330     case ARG_PLANES:
331     {
332       gint new_n_planes = g_value_get_int (value);
333       GstBuffer **new_planetable;
334       gint i;
335
336       /* If the number of planes changed, copy across any existing planes */
337       if (new_n_planes != filter->planes) {
338         new_planetable =
339             (GstBuffer **) g_malloc (new_n_planes * sizeof (GstBuffer *));
340
341         for (i = 0; (i < new_n_planes) && (i < filter->planes); i++) {
342           new_planetable[i] = filter->planetable[i];
343         }
344         for (; i < filter->planes; i++) {
345           if (filter->planetable[i])
346             gst_buffer_unref (filter->planetable[i]);
347         }
348         g_free (filter->planetable);
349         filter->planetable = new_planetable;
350         filter->current_plane = filter->planes - 1;
351         filter->planes = new_n_planes;
352       }
353     }
354       break;
355     default:
356       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357       break;
358   }
359 }
360
361 static void
362 gst_quarktv_get_property (GObject * object, guint prop_id, GValue * value,
363     GParamSpec * pspec)
364 {
365   GstQuarkTV *filter;
366
367   g_return_if_fail (GST_IS_QUARKTV (object));
368
369   filter = GST_QUARKTV (object);
370
371   switch (prop_id) {
372     case ARG_PLANES:
373       g_value_set_int (value, filter->planes);
374       break;
375     default:
376       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377       break;
378   }
379 }