gst-indent
[platform/upstream/gst-plugins-good.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     type = g_type_register_static (GST_TYPE_ELEMENT, "GstGOOM", &info, 0);
138   }
139   return type;
140 }
141
142 static void
143 gst_goom_base_init (GstGOOMClass * klass)
144 {
145   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
146
147   gst_element_class_set_details (element_class, &gst_goom_details);
148   gst_element_class_add_pad_template (element_class,
149       gst_static_pad_template_get (&sink_template));
150   gst_element_class_add_pad_template (element_class,
151       gst_static_pad_template_get (&src_template));
152 }
153
154 static void
155 gst_goom_class_init (GstGOOMClass * klass)
156 {
157   GObjectClass *gobject_class;
158   GstElementClass *gstelement_class;
159
160   gobject_class = (GObjectClass *) klass;
161   gstelement_class = (GstElementClass *) klass;
162
163   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
164
165   gobject_class->dispose = gst_goom_dispose;
166
167   gstelement_class->change_state = gst_goom_change_state;
168 }
169
170 static void
171 gst_goom_init (GstGOOM * goom)
172 {
173   /* create the sink and src pads */
174   goom->sinkpad =
175       gst_pad_new_from_template (gst_static_pad_template_get (&sink_template),
176       "sink");
177   goom->srcpad =
178       gst_pad_new_from_template (gst_static_pad_template_get (&src_template),
179       "src");
180   gst_element_add_pad (GST_ELEMENT (goom), goom->sinkpad);
181   gst_element_add_pad (GST_ELEMENT (goom), goom->srcpad);
182
183   GST_FLAG_SET (goom, GST_ELEMENT_EVENT_AWARE);
184
185   gst_pad_set_chain_function (goom->sinkpad, gst_goom_chain);
186   gst_pad_set_link_function (goom->sinkpad, gst_goom_sinkconnect);
187
188   gst_pad_set_link_function (goom->srcpad, gst_goom_srcconnect);
189   gst_pad_set_fixate_function (goom->srcpad, gst_goom_src_fixate);
190
191   goom->width = 320;
192   goom->height = 200;
193   goom->fps = 25.;              /* desired frame rate */
194   goom->channels = 0;
195   /* set to something */
196   goom_init (50, 50);
197 }
198
199 static void
200 gst_goom_dispose (GObject * object)
201 {
202   goom_close ();
203
204   G_OBJECT_CLASS (parent_class)->dispose (object);
205 }
206
207 static GstPadLinkReturn
208 gst_goom_sinkconnect (GstPad * pad, const GstCaps * caps)
209 {
210   GstGOOM *goom;
211   GstStructure *structure;
212
213   goom = GST_GOOM (gst_pad_get_parent (pad));
214
215   structure = gst_caps_get_structure (caps, 0);
216
217   gst_structure_get_int (structure, "channels", &goom->channels);
218
219   return GST_PAD_LINK_OK;
220 }
221
222 static GstPadLinkReturn
223 gst_goom_srcconnect (GstPad * pad, const GstCaps * caps)
224 {
225   GstGOOM *goom;
226   GstStructure *structure;
227
228   goom = GST_GOOM (gst_pad_get_parent (pad));
229
230   structure = gst_caps_get_structure (caps, 0);
231
232   gst_structure_get_int (structure, "width", &goom->width);
233   gst_structure_get_int (structure, "height", &goom->height);
234   gst_structure_get_double (structure, "framerate", &goom->fps);
235
236   goom_set_resolution (goom->width, goom->height);
237   goom->srcnegotiated = TRUE;
238
239   return GST_PAD_LINK_OK;
240 }
241
242 static GstCaps *
243 gst_goom_src_fixate (GstPad * pad, const GstCaps * caps)
244 {
245   GstCaps *newcaps;
246   GstStructure *structure;
247
248   if (!gst_caps_is_simple (caps))
249     return NULL;
250
251   newcaps = gst_caps_copy (caps);
252   structure = gst_caps_get_structure (newcaps, 0);
253
254   if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 320)) {
255     return newcaps;
256   }
257   if (gst_caps_structure_fixate_field_nearest_int (structure, "height", 240)) {
258     return newcaps;
259   }
260   if (gst_caps_structure_fixate_field_nearest_double (structure, "framerate",
261           30.0)) {
262     return newcaps;
263   }
264
265   /* failed to fixate */
266   gst_caps_free (newcaps);
267   return NULL;
268 }
269
270 static void
271 gst_goom_chain (GstPad * pad, GstData * _data)
272 {
273   GstBuffer *bufin = GST_BUFFER (_data);
274   GstGOOM *goom;
275   GstBuffer *bufout;
276   guint32 samples_in;
277   gint16 *data;
278   gint i;
279
280   goom = GST_GOOM (gst_pad_get_parent (pad));
281
282   GST_DEBUG ("GOOM: chainfunc called");
283
284   if (GST_IS_EVENT (bufin)) {
285     GstEvent *event = GST_EVENT (bufin);
286
287     switch (GST_EVENT_TYPE (event)) {
288       case GST_EVENT_DISCONTINUOUS:
289       {
290         gint64 value = 0;
291
292         gst_event_discont_get_value (event, GST_FORMAT_TIME, &value);
293
294         goom->next_time = value;
295       }
296       default:
297         gst_pad_event_default (pad, event);
298         break;
299     }
300     return;
301   }
302
303   if (goom->channels == 0) {
304     GST_ELEMENT_ERROR (goom, CORE, NEGOTIATION, (NULL),
305         ("format wasn't negotiated before chain function"));
306
307     goto done;
308   }
309
310   if (!GST_PAD_IS_USABLE (goom->srcpad))
311     goto done;
312
313   samples_in = GST_BUFFER_SIZE (bufin) / (sizeof (gint16) * goom->channels);
314
315   GST_DEBUG ("input buffer has %d samples", samples_in);
316
317   if (GST_BUFFER_TIMESTAMP (bufin) < goom->next_time || samples_in < 512) {
318     goto done;
319   }
320
321   data = (gint16 *) GST_BUFFER_DATA (bufin);
322   if (goom->channels == 2) {
323     for (i = 0; i < 512; i++) {
324       goom->datain[0][i] = *data++;
325       goom->datain[1][i] = *data++;
326     }
327   } else {
328     for (i = 0; i < 512; i++) {
329       goom->datain[0][i] = *data;
330       goom->datain[1][i] = *data++;
331     }
332   }
333
334   bufout = gst_buffer_new ();
335   GST_BUFFER_SIZE (bufout) = goom->width * goom->height * 4;
336   GST_BUFFER_DATA (bufout) = (guchar *) goom_update (goom->datain);
337   GST_BUFFER_TIMESTAMP (bufout) = goom->next_time;
338   GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
339
340   goom->next_time += GST_SECOND / goom->fps;
341
342   gst_pad_push (goom->srcpad, GST_DATA (bufout));
343
344 done:
345   gst_buffer_unref (bufin);
346
347   GST_DEBUG ("GOOM: exiting chainfunc");
348 }
349
350 static GstElementStateReturn
351 gst_goom_change_state (GstElement * element)
352 {
353   GstGOOM *goom = GST_GOOM (element);
354
355   switch (GST_STATE_TRANSITION (element)) {
356     case GST_STATE_NULL_TO_READY:
357       break;
358     case GST_STATE_READY_TO_NULL:
359       break;
360     case GST_STATE_READY_TO_PAUSED:
361       goom->next_time = 0;
362       goom->srcnegotiated = FALSE;
363       break;
364     case GST_STATE_PAUSED_TO_READY:
365       goom->channels = 0;
366       break;
367     default:
368       break;
369   }
370
371   if (GST_ELEMENT_CLASS (parent_class)->change_state)
372     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
373
374   return GST_STATE_SUCCESS;
375 }
376
377 static gboolean
378 plugin_init (GstPlugin * plugin)
379 {
380   return gst_element_register (plugin, "goom", GST_RANK_NONE, GST_TYPE_GOOM);
381 }
382
383 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
384     GST_VERSION_MINOR,
385     "goom",
386     "GOOM visualization filter",
387     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)