Merge CAPS branch
[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_ULAW,GstQuarkTV))
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 (
70   "QuarkTV",
71   "Filter/Effect/Video",
72   "Motion dissolver",
73   "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>"
74 );
75
76 /* Filter signals and args */
77 enum
78 {
79   /* FILL ME */
80   LAST_SIGNAL
81 };
82
83 enum
84 {
85   ARG_0,
86   ARG_PLANES,
87 };
88
89 static void     gst_quarktv_base_init           (gpointer g_class);
90 static void     gst_quarktv_class_init          (GstQuarkTVClass * klass);
91 static void     gst_quarktv_init                (GstQuarkTV * filter);
92
93 static GstElementStateReturn
94                 gst_quarktv_change_state        (GstElement *element);
95                 
96 static void     gst_quarktv_set_property        (GObject * object, guint prop_id,
97                                                  const GValue * value, GParamSpec * pspec);
98 static void     gst_quarktv_get_property        (GObject * object, guint prop_id,
99                                                  GValue * value, GParamSpec * pspec);
100
101 static void     gst_quarktv_chain               (GstPad * pad, GstData *_data);
102
103 static GstElementClass *parent_class = NULL;
104 /* static guint gst_quarktv_signals[LAST_SIGNAL] = { 0 }; */
105
106 static inline guint32
107 fastrand (void)
108 {
109   static unsigned int fastrand_val;
110
111   return (fastrand_val = fastrand_val * 1103515245 + 12345);
112 }
113
114 GType 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 = g_type_register_static (GST_TYPE_ELEMENT, "GstQuarkTV", &quarktv_info, 0);
132   }
133   return quarktv_type;
134 }
135
136 static void
137 gst_quarktv_base_init (gpointer g_class)
138 {
139   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
140
141   gst_element_class_add_pad_template (element_class,
142       gst_static_pad_template_get (&gst_effectv_src_template));
143   gst_element_class_add_pad_template (element_class,
144       gst_static_pad_template_get (&gst_effectv_sink_template));
145  
146   gst_element_class_set_details (element_class, &gst_quarktv_details);
147 }
148
149 static void
150 gst_quarktv_class_init (GstQuarkTVClass * klass)
151 {
152   GObjectClass *gobject_class;
153   GstElementClass *gstelement_class;
154
155   gobject_class = (GObjectClass *) klass;
156   gstelement_class = (GstElementClass *) klass;
157
158   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
159
160   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PLANES,
161     g_param_spec_int ("planes","Planes","Number of frames in the buffer",
162                       1, 32, PLANES, G_PARAM_READWRITE));
163        
164   gobject_class->set_property = gst_quarktv_set_property;
165   gobject_class->get_property = gst_quarktv_get_property;
166
167   gstelement_class->change_state = gst_quarktv_change_state;
168 }
169
170 static GstPadLinkReturn
171 gst_quarktv_sinkconnect (GstPad * pad, const GstCaps * caps)
172 {
173   GstQuarkTV *filter;
174   gint i;
175   GstStructure *structure;
176
177   filter = GST_QUARKTV (gst_pad_get_parent (pad));
178
179   structure = gst_caps_get_structure (caps, 0);
180
181   gst_structure_get_int  (structure, "width", &filter->width);
182   gst_structure_get_int  (structure, "height", &filter->height);
183
184   filter->area = filter->width * filter->height;
185
186   g_free (filter->planetable);
187   filter->planetable = (GstBuffer **) g_malloc(filter->planes * sizeof(GstBuffer *));
188
189   for(i = 0; i < filter->planes; i++) {
190     filter->planetable[i] = NULL;
191   }
192
193   return gst_pad_try_set_caps (filter->srcpad, caps);
194 }
195
196 static void
197 gst_quarktv_init (GstQuarkTV * filter)
198 {
199   filter->sinkpad = gst_pad_new_from_template (
200       gst_static_pad_template_get(&gst_effectv_sink_template), "sink");
201   gst_pad_set_chain_function (filter->sinkpad, gst_quarktv_chain);
202   gst_pad_set_link_function (filter->sinkpad, gst_quarktv_sinkconnect);
203   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
204
205   filter->srcpad = gst_pad_new_from_template (
206       gst_static_pad_template_get(&gst_effectv_src_template), "src");
207   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
208
209   filter->planes = PLANES;
210   filter->current_plane = filter->planes - 1;
211   filter->planetable = NULL;
212 }
213
214 static void
215 gst_quarktv_chain (GstPad * pad, GstData *_data)
216 {
217   GstBuffer *buf = GST_BUFFER (_data);
218   GstQuarkTV *filter;
219   guint32 *src, *dest;
220   GstBuffer *outbuf;
221   gint area;
222
223   filter = GST_QUARKTV (gst_pad_get_parent (pad));
224
225   src = (guint32 *) GST_BUFFER_DATA (buf);
226
227   area = filter->area;
228
229   outbuf = gst_buffer_new ();
230   GST_BUFFER_SIZE (outbuf) = area * sizeof(guint32);
231   dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
232   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
233   
234   if (filter->planetable[filter->current_plane]) 
235     gst_buffer_unref (filter->planetable[filter->current_plane]);
236
237   filter->planetable[filter->current_plane] = buf;
238
239   while (--area) {
240     GstBuffer *rand;
241     
242     /* pick a random buffer */
243     rand = filter->planetable[(filter->current_plane + (fastrand () >> 24)) & (filter->planes - 1)];
244     
245     dest[area] = (rand ? ((guint32 *)GST_BUFFER_DATA (rand))[area] : 0);
246   }
247
248   gst_pad_push (filter->srcpad, GST_DATA (outbuf));
249
250   filter->current_plane--;
251   
252   if (filter->current_plane < 0) 
253     filter->current_plane = filter->planes - 1;
254 }
255
256 static GstElementStateReturn
257 gst_quarktv_change_state (GstElement *element)
258
259   GstQuarkTV *filter = GST_QUARKTV (element);
260
261   switch (GST_STATE_TRANSITION (element)) {
262     case GST_STATE_PAUSED_TO_READY:
263     {
264       gint i;
265
266       for (i = 0; i < filter->planes; i++) {
267         if (filter->planetable[i])
268           gst_buffer_unref (filter->planetable[i]);
269         filter->planetable[i] = NULL;
270       }
271       g_free (filter->planetable);
272       filter->planetable = NULL;
273       break;
274     }
275     default:
276       break;
277   }
278
279   return GST_ELEMENT_CLASS (parent_class)->change_state (element);
280 }
281
282
283 static void
284 gst_quarktv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
285 {
286   GstQuarkTV *filter;
287
288   /* it's not null if we got it, but it might not be ours */
289   g_return_if_fail (GST_IS_QUARKTV (object));
290
291   filter = GST_QUARKTV (object);
292
293   switch (prop_id) {
294     case ARG_PLANES:
295       filter->planes = g_value_get_int (value);
296       filter->current_plane = filter->planes - 1;
297       break;
298     default:
299       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300       break;
301   }
302 }
303
304 static void
305 gst_quarktv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
306 {
307   GstQuarkTV *filter;
308
309   /* it's not null if we got it, but it might not be ours */
310   g_return_if_fail (GST_IS_QUARKTV (object));
311
312   filter = GST_QUARKTV (object);
313
314   switch (prop_id) {
315     case ARG_PLANES:
316       g_value_set_int (value, filter->planes);
317       break;
318     default:
319       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
320       break;
321   }
322 }