Remove the memcpy
[platform/upstream/gst-plugins-good.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. We release this product under the terms of the
8  * GNU General Public License version 2. The license is included in the file
9  * COPYING.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14  */
15
16 #include <math.h>
17 #include <string.h>
18 #include <gst/gst.h>
19 #include "gsteffectv.h"
20
21 #define GST_TYPE_QUARKTV \
22   (gst_quarktv_get_type())
23 #define GST_QUARKTV(obj) \
24   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_QUARKTV,GstQuarkTV))
25 #define GST_QUARKTV_CLASS(klass) \
26   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ULAW,GstQuarkTV))
27 #define GST_IS_QUARKTV(obj) \
28   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_QUARKTV))
29 #define GST_IS_QUARKTV_CLASS(obj) \
30   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_QUARKTV))
31
32 /* number of frames of time-buffer. It should be as a configurable paramter */
33 /* This number also must be 2^n just for the speed. */
34 #define PLANES 16
35
36 typedef struct _GstQuarkTV GstQuarkTV;
37 typedef struct _GstQuarkTVClass GstQuarkTVClass;
38
39 struct _GstQuarkTV
40 {
41   GstElement element;
42
43   GstPad *sinkpad, *srcpad;
44
45   gint width, height;
46   gint area;
47   gint planes;
48   gint current_plane;
49   GstBuffer **planetable;
50 };
51
52 struct _GstQuarkTVClass
53 {
54   GstElementClass parent_class;
55 };
56
57 GstElementDetails gst_quarktv_details = {
58   "QuarkTV",
59   "Filter/Effect",
60   "Motion disolver",
61   VERSION,
62   "FUKUCHI, Kentarou <fukuchi@users.sourceforge.net>",
63   "(C) 2001 FUKUCHI Kentarou",
64 };
65
66
67 /* Filter signals and args */
68 enum
69 {
70   /* FILL ME */
71   LAST_SIGNAL
72 };
73
74 enum
75 {
76   ARG_0,
77   ARG_PLANES,
78 };
79
80 static void     gst_quarktv_class_init          (GstQuarkTVClass * klass);
81 static void     gst_quarktv_init                (GstQuarkTV * filter);
82
83 static GstElementStateReturn
84                 gst_quarktv_change_state        (GstElement *element);
85                 
86 static void     gst_quarktv_set_property        (GObject * object, guint prop_id,
87                                                  const GValue * value, GParamSpec * pspec);
88 static void     gst_quarktv_get_property        (GObject * object, guint prop_id,
89                                                  GValue * value, GParamSpec * pspec);
90
91 static void     gst_quarktv_chain               (GstPad * pad, GstBuffer * buf);
92
93 static GstElementClass *parent_class = NULL;
94 /* static guint gst_quarktv_signals[LAST_SIGNAL] = { 0 }; */
95
96 static inline guint32
97 fastrand (void)
98 {
99   static unsigned int fastrand_val;
100
101   return (fastrand_val = fastrand_val * 1103515245 + 12345);
102 }
103
104 GType gst_quarktv_get_type (void)
105 {
106   static GType quarktv_type = 0;
107
108   if (!quarktv_type) {
109     static const GTypeInfo quarktv_info = {
110       sizeof (GstQuarkTVClass), 
111       NULL,
112       NULL,
113       (GClassInitFunc) gst_quarktv_class_init,
114       NULL,
115       NULL,
116       sizeof (GstQuarkTV),
117       0,
118       (GInstanceInitFunc) gst_quarktv_init,
119     };
120
121     quarktv_type = g_type_register_static (GST_TYPE_ELEMENT, "GstQuarkTV", &quarktv_info, 0);
122   }
123   return quarktv_type;
124 }
125
126 static void
127 gst_quarktv_class_init (GstQuarkTVClass * klass)
128 {
129   GObjectClass *gobject_class;
130   GstElementClass *gstelement_class;
131
132   gobject_class = (GObjectClass *) klass;
133   gstelement_class = (GstElementClass *) klass;
134
135   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
136
137   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PLANES,
138     g_param_spec_int ("planes","Planes","Number of frames in the buffer",
139                       1, 32, PLANES, G_PARAM_READWRITE));
140        
141   gobject_class->set_property = gst_quarktv_set_property;
142   gobject_class->get_property = gst_quarktv_get_property;
143
144   gstelement_class->change_state = gst_quarktv_change_state;
145 }
146
147 static GstPadConnectReturn
148 gst_quarktv_sinkconnect (GstPad * pad, GstCaps * caps)
149 {
150   GstQuarkTV *filter;
151   gint i;
152
153   filter = GST_QUARKTV (gst_pad_get_parent (pad));
154
155   if (!GST_CAPS_IS_FIXED (caps))
156     return GST_PAD_CONNECT_DELAYED;
157
158   gst_caps_get_int (caps, "width", &filter->width);
159   gst_caps_get_int (caps, "height", &filter->height);
160
161   filter->area = filter->width * filter->height;
162
163   g_free (filter->planetable);
164   filter->planetable = (GstBuffer **) g_malloc(filter->planes * sizeof(GstBuffer *));
165
166   for(i = 0; i < filter->planes; i++) {
167     filter->planetable[i] = NULL;
168   }
169
170   if (gst_pad_try_set_caps (filter->srcpad, caps)) {
171     return GST_PAD_CONNECT_OK;
172   }
173
174   return GST_PAD_CONNECT_REFUSED;
175 }
176
177 static void
178 gst_quarktv_init (GstQuarkTV * filter)
179 {
180   filter->sinkpad = gst_pad_new_from_template (gst_effectv_sink_factory (), "sink");
181   gst_pad_set_chain_function (filter->sinkpad, gst_quarktv_chain);
182   gst_pad_set_connect_function (filter->sinkpad, gst_quarktv_sinkconnect);
183   gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
184
185   filter->srcpad = gst_pad_new_from_template (gst_effectv_src_factory (), "src");
186   gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
187
188   filter->planes = PLANES;
189   filter->current_plane = filter->planes - 1;
190   filter->planetable = NULL;
191 }
192
193 static void
194 gst_quarktv_chain (GstPad * pad, GstBuffer * buf)
195 {
196   GstQuarkTV *filter;
197   guint32 *src, *dest;
198   GstBuffer *outbuf;
199   gint area;
200
201   filter = GST_QUARKTV (gst_pad_get_parent (pad));
202
203   src = (guint32 *) GST_BUFFER_DATA (buf);
204
205   area = filter->area;
206
207   outbuf = gst_buffer_new ();
208   GST_BUFFER_SIZE (outbuf) = area * sizeof(guint32);
209   dest = (guint32 *) GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
210   GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
211   
212   if (filter->planetable[filter->current_plane]) 
213     gst_buffer_unref (filter->planetable[filter->current_plane]);
214
215   filter->planetable[filter->current_plane] = buf;
216
217   while (--area) {
218     GstBuffer *rand;
219     
220     /* pick a random buffer */
221     rand = filter->planetable[(filter->current_plane + (fastrand () >> 24)) & (filter->planes - 1)];
222     
223     dest[area] = (rand ? ((guint32 *)GST_BUFFER_DATA (rand))[area] : 0);
224   }
225
226   gst_pad_push (filter->srcpad, outbuf);
227
228   filter->current_plane--;
229   
230   if (filter->current_plane < 0) 
231     filter->current_plane = filter->planes - 1;
232 }
233
234 static GstElementStateReturn
235 gst_quarktv_change_state (GstElement *element)
236
237   GstQuarkTV *filter = GST_QUARKTV (element);
238
239   switch (GST_STATE_TRANSITION (element)) {
240     case GST_STATE_PAUSED_TO_READY:
241     {
242       gint i;
243
244       for (i = 0; i < filter->planes; i++) {
245         if (filter->planetable[i])
246           gst_buffer_unref (filter->planetable[i]);
247         filter->planetable[i] = NULL;
248       }
249       g_free (filter->planetable);
250       filter->planetable = NULL;
251       break;
252     }
253     default:
254       break;
255   }
256
257   return GST_ELEMENT_CLASS (parent_class)->change_state (element);
258 }
259
260
261 static void
262 gst_quarktv_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
263 {
264   GstQuarkTV *filter;
265
266   /* it's not null if we got it, but it might not be ours */
267   g_return_if_fail (GST_IS_QUARKTV (object));
268
269   filter = GST_QUARKTV (object);
270
271   switch (prop_id) {
272     case ARG_PLANES:
273       filter->planes = g_value_get_int (value);
274       filter->current_plane = filter->planes - 1;
275       break;
276     default:
277       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278       break;
279   }
280 }
281
282 static void
283 gst_quarktv_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
284 {
285   GstQuarkTV *filter;
286
287   /* it's not null if we got it, but it might not be ours */
288   g_return_if_fail (GST_IS_QUARKTV (object));
289
290   filter = GST_QUARKTV (object);
291
292   switch (prop_id) {
293     case ARG_PLANES:
294       g_value_set_int (value, filter->planes);
295       break;
296     default:
297       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298       break;
299   }
300 }