remove copyright field from plugins
[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   gfloat 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 GST_PAD_TEMPLATE_FACTORY (src_template,
82   "src",
83   GST_PAD_SRC,
84   GST_PAD_ALWAYS,
85   gst_caps_new (
86     "goomsrc",
87     "video/x-raw-rgb",
88     GST_VIDEO_RGB_PAD_TEMPLATE_PROPS_32
89   )
90 )
91
92 GST_PAD_TEMPLATE_FACTORY (sink_template,
93   "sink",                               /* the name of the pads */
94   GST_PAD_SINK,                         /* type of the pad */
95   GST_PAD_ALWAYS,                       /* ALWAYS/SOMETIMES */
96   GST_CAPS_NEW (
97     "goomsink",                         /* the name of the caps */
98     "audio/x-raw-int",                  /* the mime type of the caps */
99        /* Properties follow: */
100       "endianness", GST_PROPS_INT (G_BYTE_ORDER),
101       "signed",     GST_PROPS_BOOLEAN (TRUE),
102       "width",      GST_PROPS_INT (16),
103       "depth",      GST_PROPS_INT (16),
104       "rate",       GST_PROPS_INT_RANGE (8000, 96000),
105       "channels",   GST_PROPS_INT_RANGE (1, 2)
106   )
107 )
108
109
110 static void             gst_goom_class_init     (GstGOOMClass *klass);
111 static void             gst_goom_base_init      (GstGOOMClass *klass);
112 static void             gst_goom_init           (GstGOOM *goom);
113 static void             gst_goom_dispose        (GObject *object);
114
115 static GstElementStateReturn
116                         gst_goom_change_state   (GstElement *element);
117
118 static void             gst_goom_chain          (GstPad *pad, GstData *_data);
119
120 static GstPadLinkReturn gst_goom_sinkconnect    (GstPad *pad, GstCaps *caps);
121 static GstPadLinkReturn gst_goom_srcconnect     (GstPad *pad, GstCaps *caps);
122
123 static GstElementClass *parent_class = NULL;
124
125 GType
126 gst_goom_get_type (void)
127 {
128   static GType type = 0;
129
130   if (!type) {
131     static const GTypeInfo info = {
132       sizeof (GstGOOMClass),      
133       (GBaseInitFunc) gst_goom_base_init,      
134       NULL,      
135       (GClassInitFunc) gst_goom_class_init,
136       NULL,
137       NULL,
138       sizeof (GstGOOM),
139       0,
140       (GInstanceInitFunc) gst_goom_init,
141     };
142     type = g_type_register_static (GST_TYPE_ELEMENT, "GstGOOM", &info, 0);
143   }
144   return type;
145 }
146
147 static void
148 gst_goom_base_init (GstGOOMClass *klass)
149 {
150   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
151
152   gst_element_class_set_details (element_class, &gst_goom_details);
153   gst_element_class_add_pad_template (element_class,
154         GST_PAD_TEMPLATE_GET (sink_template));
155   gst_element_class_add_pad_template (element_class,
156         GST_PAD_TEMPLATE_GET (src_template));
157 }
158
159 static void
160 gst_goom_class_init(GstGOOMClass *klass)
161 {
162   GObjectClass *gobject_class;
163   GstElementClass *gstelement_class;
164
165   gobject_class = (GObjectClass*) klass;
166   gstelement_class = (GstElementClass*) klass;
167
168   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
169
170   gobject_class->dispose        = gst_goom_dispose;
171
172   gstelement_class->change_state = gst_goom_change_state;
173 }
174
175 static void
176 gst_goom_init (GstGOOM *goom)
177 {
178   /* create the sink and src pads */
179   goom->sinkpad = gst_pad_new_from_template (
180                   GST_PAD_TEMPLATE_GET (sink_template ), "sink");
181   goom->srcpad = gst_pad_new_from_template (
182                   GST_PAD_TEMPLATE_GET (src_template ), "src");
183   gst_element_add_pad (GST_ELEMENT (goom), goom->sinkpad);
184   gst_element_add_pad (GST_ELEMENT (goom), goom->srcpad);
185
186   GST_FLAG_SET (goom, GST_ELEMENT_EVENT_AWARE);
187
188   gst_pad_set_chain_function (goom->sinkpad, gst_goom_chain);
189   gst_pad_set_link_function (goom->sinkpad, gst_goom_sinkconnect);
190
191   gst_pad_set_link_function (goom->srcpad, gst_goom_srcconnect);
192
193   goom->width = 320;
194   goom->height = 200;
195   goom->fps = 25.; /* desired frame rate */
196   goom->channels = 0;
197   /* set to something */
198   goom_init (50, 50);
199 }
200
201 static void
202 gst_goom_dispose (GObject *object)
203 {
204   goom_close ();
205   
206   G_OBJECT_CLASS (parent_class)->dispose (object);
207 }
208
209 static GstPadLinkReturn
210 gst_goom_sinkconnect (GstPad *pad, GstCaps *caps)
211 {
212   GstGOOM *goom;
213   goom = GST_GOOM (gst_pad_get_parent (pad));
214
215   if (!GST_CAPS_IS_FIXED (caps)) {
216     return GST_PAD_LINK_DELAYED;
217   }
218
219   gst_caps_get_int (caps, "channels", &goom->channels);
220
221   return GST_PAD_LINK_OK;
222 }
223
224 static GstPadLinkReturn
225 gst_goom_srcconnect (GstPad *pad, GstCaps *caps)
226 {
227   GstGOOM *goom;
228   goom = GST_GOOM (gst_pad_get_parent (pad));
229
230   if (!GST_CAPS_IS_FIXED (caps)) {
231     return GST_PAD_LINK_DELAYED;
232   }
233
234   if (gst_caps_has_property_typed (caps, "width", GST_PROPS_INT_TYPE)) {
235     gst_caps_get_int (caps, "width", &goom->width);
236   }
237   if (gst_caps_has_property_typed (caps, "height", GST_PROPS_INT_TYPE)) {
238     gst_caps_get_int (caps, "height", &goom->height);
239   }
240   if (gst_caps_has_property_typed (caps, "framerate", GST_PROPS_FLOAT_TYPE)) {
241     gst_caps_get_float (caps, "framerate", &goom->fps);
242   }
243
244   goom_set_resolution (goom->width, goom->height);
245   goom->srcnegotiated = TRUE;
246
247   return GST_PAD_LINK_OK;
248 }
249
250 static gboolean
251 gst_goom_negotiate_default (GstGOOM *goom)
252 {
253   GstCaps *caps;
254
255   caps = GST_CAPS_NEW (
256              "goomsrc",
257              "video/x-raw-rgb",
258                "format",        GST_PROPS_FOURCC (GST_STR_FOURCC ("RGB ")), 
259                "bpp",           GST_PROPS_INT (32), 
260                "depth",         GST_PROPS_INT (32), 
261                "endianness",    GST_PROPS_INT (G_BIG_ENDIAN),
262                "red_mask",      GST_PROPS_INT (R_MASK_32), 
263                "green_mask",    GST_PROPS_INT (G_MASK_32), 
264                "blue_mask",     GST_PROPS_INT (B_MASK_32), 
265                "width",         GST_PROPS_INT (goom->width), 
266                "height",        GST_PROPS_INT (goom->height),
267                "framerate",     GST_PROPS_FLOAT (goom->fps)
268            );
269
270   if (gst_pad_try_set_caps (goom->srcpad, caps) <= 0) {
271     return FALSE;
272   }
273
274   goom_set_resolution (goom->width, goom->height);
275   goom->srcnegotiated = TRUE;
276
277   return TRUE;
278 }
279
280 static void
281 gst_goom_chain (GstPad *pad, GstData *_data)
282 {
283   GstBuffer *bufin = GST_BUFFER (_data);
284   GstGOOM *goom;
285   GstBuffer *bufout;
286   guint32 samples_in;
287   gint16 *data;
288   gint i;
289
290   goom = GST_GOOM (gst_pad_get_parent (pad));
291
292   GST_DEBUG ("GOOM: chainfunc called");
293
294   if (GST_IS_EVENT (bufin)) {
295     GstEvent *event = GST_EVENT (bufin);
296
297     switch (GST_EVENT_TYPE (event)) {
298       case GST_EVENT_DISCONTINUOUS:
299       {
300         gint64 value = 0;
301
302         gst_event_discont_get_value (event, GST_FORMAT_TIME, &value);
303
304         goom->next_time = value;
305       }
306       default:
307         gst_pad_event_default (pad, event);
308         break;
309     }
310     return;
311   }
312
313   if (goom->channels == 0) {
314     gst_element_error (GST_ELEMENT (goom), "sink format not negotiated");
315     goto done;
316   }
317
318   if (!GST_PAD_IS_USABLE (goom->srcpad))
319     goto done;
320
321   if (!goom->srcnegotiated) {
322     if (!gst_goom_negotiate_default (goom)) {
323       gst_element_error (GST_ELEMENT (goom), "could not negotiate src format");
324       goto done;
325     }
326   }
327
328   samples_in = GST_BUFFER_SIZE (bufin) / (sizeof (gint16) * goom->channels);
329
330   GST_DEBUG ("input buffer has %d samples", samples_in);
331
332   if (GST_BUFFER_TIMESTAMP (bufin) < goom->next_time || samples_in < 512) {
333     goto done;
334   }
335
336   data = (gint16 *) GST_BUFFER_DATA (bufin);
337   if (goom->channels == 2) {
338     for (i=0; i < 512; i++) {
339       goom->datain[0][i] = *data++;
340       goom->datain[1][i] = *data++;
341     }
342   }
343   else {
344     for (i=0; i < 512; i++) {
345       goom->datain[0][i] = *data;
346       goom->datain[1][i] = *data++;
347     }
348   }
349
350   bufout = gst_buffer_new ();
351   GST_BUFFER_SIZE (bufout) = goom->width * goom->height * 4;
352   GST_BUFFER_DATA (bufout) = (guchar *) goom_update (goom->datain);
353   GST_BUFFER_TIMESTAMP (bufout) = goom->next_time;
354   GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
355
356   goom->next_time += GST_SECOND / goom->fps;
357
358   gst_pad_push (goom->srcpad, GST_DATA (bufout));
359
360 done:
361   gst_buffer_unref (bufin);
362
363   GST_DEBUG ("GOOM: exiting chainfunc");
364 }
365
366 static GstElementStateReturn
367 gst_goom_change_state (GstElement *element)
368
369   GstGOOM *goom = GST_GOOM (element);
370
371   switch (GST_STATE_TRANSITION (element)) {
372     case GST_STATE_NULL_TO_READY:
373       break;
374     case GST_STATE_READY_TO_NULL:
375       break; 
376     case GST_STATE_READY_TO_PAUSED:
377       goom->next_time = 0;
378       goom->srcnegotiated = FALSE;
379       goom->channels = 0;
380       break;
381     case GST_STATE_PAUSED_TO_READY:
382       break;
383     default:
384       break;
385   }
386
387   if (GST_ELEMENT_CLASS (parent_class)->change_state)
388     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
389
390   return GST_STATE_SUCCESS;
391 }
392
393 static gboolean
394 plugin_init (GstPlugin *plugin)
395 {
396   return gst_element_register (plugin, "goom",
397                                GST_RANK_NONE, GST_TYPE_GOOM);
398 }
399
400 GST_PLUGIN_DEFINE (
401   GST_VERSION_MAJOR,
402   GST_VERSION_MINOR,
403   "goom",
404   "GOOM visualization filter",
405   plugin_init,
406   VERSION,
407   GST_LICENSE,
408   GST_PACKAGE,
409   GST_ORIGIN
410 )