don't mix tabs and spaces
[platform/upstream/gstreamer.git] / gst / goom / gstgoom.c
1 /* gstgoom.c: implementation of goom drawing element
2  * Copyright (C) <2001> Richard Boulton <richard@tartarus.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25 #include <gst/video/video.h>
26 #include "goom_core.h"
27
28 #define GST_TYPE_GOOM (gst_goom_get_type())
29 #define GST_GOOM(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_GOOM,GstGOOM))
30 #define GST_GOOM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_GOOM,GstGOOM))
31 #define GST_IS_GOOM(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_GOOM))
32 #define GST_IS_GOOM_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_GOOM))
33
34 typedef struct _GstGOOM GstGOOM;
35 typedef struct _GstGOOMClass GstGOOMClass;
36
37 struct _GstGOOM
38 {
39   GstElement element;
40
41   /* pads */
42   GstPad *sinkpad, *srcpad;
43
44   /* the timestamp of the next frame */
45   guint64 next_time;
46   gint16 datain[2][512];
47
48   /* video state */
49   gdouble fps;
50   gint width;
51   gint height;
52   gint channels;
53   gboolean srcnegotiated;
54 };
55
56 struct _GstGOOMClass
57 {
58   GstElementClass parent_class;
59 };
60
61 GType gst_goom_get_type (void);
62
63
64 /* elementfactory information */
65 static GstElementDetails gst_goom_details = {
66   "GOOM: what a GOOM!",
67   "Visualization",
68   "Takes frames of data and outputs video frames using the GOOM filter",
69   "Wim Taymans <wim.taymans@chello.be>"
70 };
71
72 /* signals and args */
73 enum
74 {
75   /* FILL ME */
76   LAST_SIGNAL
77 };
78
79 enum
80 {
81   ARG_0,
82   /* FILL ME */
83 };
84
85 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
86     GST_PAD_SRC,
87     GST_PAD_ALWAYS,
88     GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
89     );
90
91 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",    /* the name of the pads */
92     GST_PAD_SINK,               /* type of the pad */
93     GST_PAD_ALWAYS,             /* ALWAYS/SOMETIMES */
94     GST_STATIC_CAPS ("audio/x-raw-int, "
95         "endianness = (int) BYTE_ORDER, "
96         "signed = (boolean) TRUE, "
97         "width = (int) 16, "
98         "depth = (int) 16, "
99         "rate = (int) [ 8000, 96000 ], " "channels = (int) [ 1, 2 ]")
100     );
101
102
103 static void gst_goom_class_init (GstGOOMClass * klass);
104 static void gst_goom_base_init (GstGOOMClass * klass);
105 static void gst_goom_init (GstGOOM * goom);
106 static void gst_goom_dispose (GObject * object);
107
108 static GstElementStateReturn gst_goom_change_state (GstElement * element);
109
110 static void gst_goom_chain (GstPad * pad, GstData * _data);
111
112 static GstPadLinkReturn gst_goom_sinkconnect (GstPad * pad,
113     const GstCaps * caps);
114 static GstPadLinkReturn gst_goom_srcconnect (GstPad * pad,
115     const GstCaps * caps);
116 static GstCaps *gst_goom_src_fixate (GstPad * pad, const GstCaps * caps);
117
118 static GstElementClass *parent_class = NULL;
119
120 GType
121 gst_goom_get_type (void)
122 {
123   static GType type = 0;
124
125   if (!type) {
126     static const GTypeInfo info = {
127       sizeof (GstGOOMClass),
128       (GBaseInitFunc) gst_goom_base_init,
129       NULL,
130       (GClassInitFunc) gst_goom_class_init,
131       NULL,
132       NULL,
133       sizeof (GstGOOM),
134       0,
135       (GInstanceInitFunc) gst_goom_init,
136     };
137
138     type = g_type_register_static (GST_TYPE_ELEMENT, "GstGOOM", &info, 0);
139   }
140   return type;
141 }
142
143 static void
144 gst_goom_base_init (GstGOOMClass * klass)
145 {
146   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
147
148   gst_element_class_set_details (element_class, &gst_goom_details);
149   gst_element_class_add_pad_template (element_class,
150       gst_static_pad_template_get (&sink_template));
151   gst_element_class_add_pad_template (element_class,
152       gst_static_pad_template_get (&src_template));
153 }
154
155 static void
156 gst_goom_class_init (GstGOOMClass * klass)
157 {
158   GObjectClass *gobject_class;
159   GstElementClass *gstelement_class;
160
161   gobject_class = (GObjectClass *) klass;
162   gstelement_class = (GstElementClass *) klass;
163
164   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
165
166   gobject_class->dispose = gst_goom_dispose;
167
168   gstelement_class->change_state = gst_goom_change_state;
169 }
170
171 static void
172 gst_goom_init (GstGOOM * goom)
173 {
174   /* create the sink and src pads */
175   goom->sinkpad =
176       gst_pad_new_from_template (gst_static_pad_template_get (&sink_template),
177       "sink");
178   goom->srcpad =
179       gst_pad_new_from_template (gst_static_pad_template_get (&src_template),
180       "src");
181   gst_element_add_pad (GST_ELEMENT (goom), goom->sinkpad);
182   gst_element_add_pad (GST_ELEMENT (goom), goom->srcpad);
183
184   GST_FLAG_SET (goom, GST_ELEMENT_EVENT_AWARE);
185
186   gst_pad_set_chain_function (goom->sinkpad, gst_goom_chain);
187   gst_pad_set_link_function (goom->sinkpad, gst_goom_sinkconnect);
188
189   gst_pad_set_link_function (goom->srcpad, gst_goom_srcconnect);
190   gst_pad_set_fixate_function (goom->srcpad, gst_goom_src_fixate);
191
192   goom->width = 320;
193   goom->height = 200;
194   goom->fps = 25.;              /* desired frame rate */
195   goom->channels = 0;
196   /* set to something */
197   goom_init (50, 50);
198 }
199
200 static void
201 gst_goom_dispose (GObject * object)
202 {
203   goom_close ();
204
205   G_OBJECT_CLASS (parent_class)->dispose (object);
206 }
207
208 static GstPadLinkReturn
209 gst_goom_sinkconnect (GstPad * pad, const GstCaps * caps)
210 {
211   GstGOOM *goom;
212   GstStructure *structure;
213
214   goom = GST_GOOM (gst_pad_get_parent (pad));
215
216   structure = gst_caps_get_structure (caps, 0);
217
218   gst_structure_get_int (structure, "channels", &goom->channels);
219
220   return GST_PAD_LINK_OK;
221 }
222
223 static GstPadLinkReturn
224 gst_goom_srcconnect (GstPad * pad, const GstCaps * caps)
225 {
226   GstGOOM *goom;
227   GstStructure *structure;
228
229   goom = GST_GOOM (gst_pad_get_parent (pad));
230
231   structure = gst_caps_get_structure (caps, 0);
232
233   gst_structure_get_int (structure, "width", &goom->width);
234   gst_structure_get_int (structure, "height", &goom->height);
235   gst_structure_get_double (structure, "framerate", &goom->fps);
236
237   goom_set_resolution (goom->width, goom->height);
238   goom->srcnegotiated = TRUE;
239
240   return GST_PAD_LINK_OK;
241 }
242
243 static GstCaps *
244 gst_goom_src_fixate (GstPad * pad, const GstCaps * caps)
245 {
246   GstCaps *newcaps;
247   GstStructure *structure;
248
249   if (!gst_caps_is_simple (caps))
250     return NULL;
251
252   newcaps = gst_caps_copy (caps);
253   structure = gst_caps_get_structure (newcaps, 0);
254
255   if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 320)) {
256     return newcaps;
257   }
258   if (gst_caps_structure_fixate_field_nearest_int (structure, "height", 240)) {
259     return newcaps;
260   }
261   if (gst_caps_structure_fixate_field_nearest_double (structure, "framerate",
262           30.0)) {
263     return newcaps;
264   }
265
266   /* failed to fixate */
267   gst_caps_free (newcaps);
268   return NULL;
269 }
270
271 static void
272 gst_goom_chain (GstPad * pad, GstData * _data)
273 {
274   GstBuffer *bufin = GST_BUFFER (_data);
275   GstGOOM *goom;
276   GstBuffer *bufout;
277   guint32 samples_in;
278   gint16 *data;
279   gint i;
280
281   goom = GST_GOOM (gst_pad_get_parent (pad));
282
283   GST_DEBUG ("GOOM: chainfunc called");
284
285   if (GST_IS_EVENT (bufin)) {
286     GstEvent *event = GST_EVENT (bufin);
287
288     switch (GST_EVENT_TYPE (event)) {
289       case GST_EVENT_DISCONTINUOUS:
290       {
291         gint64 value = 0;
292
293         gst_event_discont_get_value (event, GST_FORMAT_TIME, &value);
294
295         goom->next_time = value;
296       }
297       default:
298         gst_pad_event_default (pad, event);
299         break;
300     }
301     return;
302   }
303
304   if (goom->channels == 0) {
305     GST_ELEMENT_ERROR (goom, CORE, NEGOTIATION, (NULL),
306         ("format wasn't negotiated before chain function"));
307
308     goto done;
309   }
310
311   if (!GST_PAD_IS_USABLE (goom->srcpad))
312     goto done;
313
314   samples_in = GST_BUFFER_SIZE (bufin) / (sizeof (gint16) * goom->channels);
315
316   GST_DEBUG ("input buffer has %d samples", samples_in);
317
318   if (GST_BUFFER_TIMESTAMP (bufin) < goom->next_time || samples_in < 512) {
319     goto done;
320   }
321
322   data = (gint16 *) GST_BUFFER_DATA (bufin);
323   if (goom->channels == 2) {
324     for (i = 0; i < 512; i++) {
325       goom->datain[0][i] = *data++;
326       goom->datain[1][i] = *data++;
327     }
328   } else {
329     for (i = 0; i < 512; i++) {
330       goom->datain[0][i] = *data;
331       goom->datain[1][i] = *data++;
332     }
333   }
334
335   bufout = gst_buffer_new ();
336   GST_BUFFER_SIZE (bufout) = goom->width * goom->height * 4;
337   GST_BUFFER_DATA (bufout) = (guchar *) goom_update (goom->datain);
338   GST_BUFFER_TIMESTAMP (bufout) = goom->next_time;
339   GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
340
341   goom->next_time += GST_SECOND / goom->fps;
342
343   gst_pad_push (goom->srcpad, GST_DATA (bufout));
344
345 done:
346   gst_buffer_unref (bufin);
347
348   GST_DEBUG ("GOOM: exiting chainfunc");
349 }
350
351 static GstElementStateReturn
352 gst_goom_change_state (GstElement * element)
353 {
354   GstGOOM *goom = GST_GOOM (element);
355
356   switch (GST_STATE_TRANSITION (element)) {
357     case GST_STATE_NULL_TO_READY:
358       break;
359     case GST_STATE_READY_TO_NULL:
360       break;
361     case GST_STATE_READY_TO_PAUSED:
362       goom->next_time = 0;
363       goom->srcnegotiated = FALSE;
364       break;
365     case GST_STATE_PAUSED_TO_READY:
366       goom->channels = 0;
367       break;
368     default:
369       break;
370   }
371
372   if (GST_ELEMENT_CLASS (parent_class)->change_state)
373     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
374
375   return GST_STATE_SUCCESS;
376 }
377
378 static gboolean
379 plugin_init (GstPlugin * plugin)
380 {
381   return gst_element_register (plugin, "goom", GST_RANK_NONE, GST_TYPE_GOOM);
382 }
383
384 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
385     GST_VERSION_MINOR,
386     "goom",
387     "GOOM visualization filter",
388     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)