change NULL to (NULL) for GST_ELEMENT_ERROR
[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   GstElement element;
39
40   /* pads */
41   GstPad *sinkpad,*srcpad;
42
43   /* the timestamp of the next frame */
44   guint64 next_time;
45   gint16 datain[2][512];
46
47   /* video state */
48   gdouble fps;
49   gint width;
50   gint height;
51   gint channels;
52   gboolean srcnegotiated;
53 };
54
55 struct _GstGOOMClass {
56   GstElementClass parent_class;
57 };
58
59 GType gst_goom_get_type(void);
60
61
62 /* elementfactory information */
63 static GstElementDetails gst_goom_details = {
64   "GOOM: what a GOOM!",
65   "Visualization",
66   "Takes frames of data and outputs video frames using the GOOM filter",
67   "Wim Taymans <wim.taymans@chello.be>"
68 };
69
70 /* signals and args */
71 enum {
72   /* FILL ME */
73   LAST_SIGNAL
74 };
75
76 enum {
77   ARG_0,
78   /* FILL ME */
79 };
80
81 static GstStaticPadTemplate src_template =
82 GST_STATIC_PAD_TEMPLATE (
83   "src",
84   GST_PAD_SRC,
85   GST_PAD_ALWAYS,
86   GST_STATIC_CAPS ( GST_VIDEO_CAPS_xRGB_HOST_ENDIAN )
87 );
88
89 static GstStaticPadTemplate sink_template =
90 GST_STATIC_PAD_TEMPLATE (
91   "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 ], "
100     "channels = (int) [ 1, 2 ]"
101   )
102 );
103
104
105 static void             gst_goom_class_init     (GstGOOMClass *klass);
106 static void             gst_goom_base_init      (GstGOOMClass *klass);
107 static void             gst_goom_init           (GstGOOM *goom);
108 static void             gst_goom_dispose        (GObject *object);
109
110 static GstElementStateReturn
111                         gst_goom_change_state   (GstElement *element);
112
113 static void             gst_goom_chain          (GstPad *pad, GstData *_data);
114
115 static GstPadLinkReturn gst_goom_sinkconnect    (GstPad *pad, const GstCaps *caps);
116 static GstPadLinkReturn gst_goom_srcconnect     (GstPad *pad, const GstCaps *caps);
117 static GstCaps *        gst_goom_src_fixate     (GstPad *pad, const GstCaps *caps);
118
119 static GstElementClass *parent_class = NULL;
120
121 GType
122 gst_goom_get_type (void)
123 {
124   static GType type = 0;
125
126   if (!type) {
127     static const GTypeInfo info = {
128       sizeof (GstGOOMClass),      
129       (GBaseInitFunc) gst_goom_base_init,      
130       NULL,      
131       (GClassInitFunc) gst_goom_class_init,
132       NULL,
133       NULL,
134       sizeof (GstGOOM),
135       0,
136       (GInstanceInitFunc) gst_goom_init,
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 = gst_pad_new_from_template (
176                   gst_static_pad_template_get (&sink_template ), "sink");
177   goom->srcpad = gst_pad_new_from_template (
178                   gst_static_pad_template_get (&src_template ), "src");
179   gst_element_add_pad (GST_ELEMENT (goom), goom->sinkpad);
180   gst_element_add_pad (GST_ELEMENT (goom), goom->srcpad);
181
182   GST_FLAG_SET (goom, GST_ELEMENT_EVENT_AWARE);
183
184   gst_pad_set_chain_function (goom->sinkpad, gst_goom_chain);
185   gst_pad_set_link_function (goom->sinkpad, gst_goom_sinkconnect);
186
187   gst_pad_set_link_function (goom->srcpad, gst_goom_srcconnect);
188   gst_pad_set_fixate_function (goom->srcpad, gst_goom_src_fixate);
189
190   goom->width = 320;
191   goom->height = 200;
192   goom->fps = 25.; /* desired frame rate */
193   goom->channels = 0;
194   /* set to something */
195   goom_init (50, 50);
196 }
197
198 static void
199 gst_goom_dispose (GObject *object)
200 {
201   goom_close ();
202   
203   G_OBJECT_CLASS (parent_class)->dispose (object);
204 }
205
206 static GstPadLinkReturn
207 gst_goom_sinkconnect (GstPad *pad, const GstCaps *caps)
208 {
209   GstGOOM *goom;
210   GstStructure *structure;
211
212   goom = GST_GOOM (gst_pad_get_parent (pad));
213
214   structure = gst_caps_get_structure (caps, 0);
215
216   gst_structure_get_int (structure, "channels", &goom->channels);
217
218   return GST_PAD_LINK_OK;
219 }
220
221 static GstPadLinkReturn
222 gst_goom_srcconnect (GstPad *pad, const GstCaps *caps)
223 {
224   GstGOOM *goom;
225   GstStructure *structure;
226
227   goom = GST_GOOM (gst_pad_get_parent (pad));
228
229   structure = gst_caps_get_structure (caps, 0);
230
231   gst_structure_get_int (structure, "width", &goom->width);
232   gst_structure_get_int (structure, "height", &goom->height);
233   gst_structure_get_double (structure, "framerate", &goom->fps);
234
235   goom_set_resolution (goom->width, goom->height);
236   goom->srcnegotiated = TRUE;
237
238   return GST_PAD_LINK_OK;
239 }
240
241 static GstCaps *
242 gst_goom_src_fixate (GstPad *pad, const GstCaps *caps)
243 {
244   GstCaps *newcaps;
245   GstStructure *structure;
246
247   if (!gst_caps_is_simple (caps)) return NULL;
248
249   newcaps = gst_caps_copy (caps);
250   structure = gst_caps_get_structure (newcaps, 0);
251
252   if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 320)) {
253     return newcaps;
254   }
255   if (gst_caps_structure_fixate_field_nearest_int (structure, "height", 240)) {
256     return newcaps;
257   }
258   if (gst_caps_structure_fixate_field_nearest_double (structure, "framerate",
259         30.0)) {
260     return newcaps;
261   }
262
263   /* failed to fixate */
264   gst_caps_free (newcaps);
265   return NULL;
266 }
267
268 static void
269 gst_goom_chain (GstPad *pad, GstData *_data)
270 {
271   GstBuffer *bufin = GST_BUFFER (_data);
272   GstGOOM *goom;
273   GstBuffer *bufout;
274   guint32 samples_in;
275   gint16 *data;
276   gint i;
277
278   goom = GST_GOOM (gst_pad_get_parent (pad));
279
280   GST_DEBUG ("GOOM: chainfunc called");
281
282   if (GST_IS_EVENT (bufin)) {
283     GstEvent *event = GST_EVENT (bufin);
284
285     switch (GST_EVENT_TYPE (event)) {
286       case GST_EVENT_DISCONTINUOUS:
287       {
288         gint64 value = 0;
289
290         gst_event_discont_get_value (event, GST_FORMAT_TIME, &value);
291
292         goom->next_time = value;
293       }
294       default:
295         gst_pad_event_default (pad, event);
296         break;
297     }
298     return;
299   }
300
301   if (goom->channels == 0) {
302     GST_ELEMENT_ERROR (goom, CORE, NEGOTIATION, (NULL),
303                        ("format wasn't negotiated before chain function"));
304
305     goto done;
306   }
307
308   if (!GST_PAD_IS_USABLE (goom->srcpad))
309     goto done;
310
311   samples_in = GST_BUFFER_SIZE (bufin) / (sizeof (gint16) * goom->channels);
312
313   GST_DEBUG ("input buffer has %d samples", samples_in);
314
315   if (GST_BUFFER_TIMESTAMP (bufin) < goom->next_time || samples_in < 512) {
316     goto done;
317   }
318
319   data = (gint16 *) GST_BUFFER_DATA (bufin);
320   if (goom->channels == 2) {
321     for (i=0; i < 512; i++) {
322       goom->datain[0][i] = *data++;
323       goom->datain[1][i] = *data++;
324     }
325   }
326   else {
327     for (i=0; i < 512; i++) {
328       goom->datain[0][i] = *data;
329       goom->datain[1][i] = *data++;
330     }
331   }
332
333   bufout = gst_buffer_new ();
334   GST_BUFFER_SIZE (bufout) = goom->width * goom->height * 4;
335   GST_BUFFER_DATA (bufout) = (guchar *) goom_update (goom->datain);
336   GST_BUFFER_TIMESTAMP (bufout) = goom->next_time;
337   GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
338
339   goom->next_time += GST_SECOND / goom->fps;
340
341   gst_pad_push (goom->srcpad, GST_DATA (bufout));
342
343 done:
344   gst_buffer_unref (bufin);
345
346   GST_DEBUG ("GOOM: exiting chainfunc");
347 }
348
349 static GstElementStateReturn
350 gst_goom_change_state (GstElement *element)
351
352   GstGOOM *goom = GST_GOOM (element);
353
354   switch (GST_STATE_TRANSITION (element)) {
355     case GST_STATE_NULL_TO_READY:
356       break;
357     case GST_STATE_READY_TO_NULL:
358       break; 
359     case GST_STATE_READY_TO_PAUSED:
360       goom->next_time = 0;
361       goom->srcnegotiated = FALSE;
362       break;
363     case GST_STATE_PAUSED_TO_READY:
364       goom->channels = 0;
365       break;
366     default:
367       break;
368   }
369
370   if (GST_ELEMENT_CLASS (parent_class)->change_state)
371     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
372
373   return GST_STATE_SUCCESS;
374 }
375
376 static gboolean
377 plugin_init (GstPlugin *plugin)
378 {
379   return gst_element_register (plugin, "goom",
380                                GST_RANK_NONE, GST_TYPE_GOOM);
381 }
382
383 GST_PLUGIN_DEFINE (
384   GST_VERSION_MAJOR,
385   GST_VERSION_MINOR,
386   "goom",
387   "GOOM visualization filter",
388   plugin_init,
389   VERSION,
390   GST_LICENSE,
391   GST_PACKAGE,
392   GST_ORIGIN
393 )