1 /* gstgoom.c: implementation of goom drawing element
2 * Copyright (C) <2001> Richard Boulton <richard@tartarus.org>
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.
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.
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.
25 #include <gst/video/video.h>
26 #include "goom_core.h"
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))
34 typedef struct _GstGOOM GstGOOM;
35 typedef struct _GstGOOMClass GstGOOMClass;
42 GstPad *sinkpad, *srcpad;
44 /* the timestamp of the next frame */
46 gint16 datain[2][512];
53 gboolean srcnegotiated;
58 GstElementClass parent_class;
61 GType gst_goom_get_type (void);
64 /* elementfactory information */
65 static GstElementDetails gst_goom_details = {
68 "Takes frames of data and outputs video frames using the GOOM filter",
69 "Wim Taymans <wim.taymans@chello.be>"
72 /* signals and args */
85 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
88 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
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, "
99 "rate = (int) [ 8000, 96000 ], " "channels = (int) [ 1, 2 ]")
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);
108 static GstElementStateReturn gst_goom_change_state (GstElement * element);
110 static void gst_goom_chain (GstPad * pad, GstData * _data);
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);
118 static GstElementClass *parent_class = NULL;
121 gst_goom_get_type (void)
123 static GType type = 0;
126 static const GTypeInfo info = {
127 sizeof (GstGOOMClass),
128 (GBaseInitFunc) gst_goom_base_init,
130 (GClassInitFunc) gst_goom_class_init,
135 (GInstanceInitFunc) gst_goom_init,
137 type = g_type_register_static (GST_TYPE_ELEMENT, "GstGOOM", &info, 0);
143 gst_goom_base_init (GstGOOMClass * klass)
145 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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));
155 gst_goom_class_init (GstGOOMClass * klass)
157 GObjectClass *gobject_class;
158 GstElementClass *gstelement_class;
160 gobject_class = (GObjectClass *) klass;
161 gstelement_class = (GstElementClass *) klass;
163 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
165 gobject_class->dispose = gst_goom_dispose;
167 gstelement_class->change_state = gst_goom_change_state;
171 gst_goom_init (GstGOOM * goom)
173 /* create the sink and src pads */
175 gst_pad_new_from_template (gst_static_pad_template_get (&sink_template),
178 gst_pad_new_from_template (gst_static_pad_template_get (&src_template),
180 gst_element_add_pad (GST_ELEMENT (goom), goom->sinkpad);
181 gst_element_add_pad (GST_ELEMENT (goom), goom->srcpad);
183 GST_FLAG_SET (goom, GST_ELEMENT_EVENT_AWARE);
185 gst_pad_set_chain_function (goom->sinkpad, gst_goom_chain);
186 gst_pad_set_link_function (goom->sinkpad, gst_goom_sinkconnect);
188 gst_pad_set_link_function (goom->srcpad, gst_goom_srcconnect);
189 gst_pad_set_fixate_function (goom->srcpad, gst_goom_src_fixate);
193 goom->fps = 25.; /* desired frame rate */
195 /* set to something */
200 gst_goom_dispose (GObject * object)
204 G_OBJECT_CLASS (parent_class)->dispose (object);
207 static GstPadLinkReturn
208 gst_goom_sinkconnect (GstPad * pad, const GstCaps * caps)
211 GstStructure *structure;
213 goom = GST_GOOM (gst_pad_get_parent (pad));
215 structure = gst_caps_get_structure (caps, 0);
217 gst_structure_get_int (structure, "channels", &goom->channels);
219 return GST_PAD_LINK_OK;
222 static GstPadLinkReturn
223 gst_goom_srcconnect (GstPad * pad, const GstCaps * caps)
226 GstStructure *structure;
228 goom = GST_GOOM (gst_pad_get_parent (pad));
230 structure = gst_caps_get_structure (caps, 0);
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);
236 goom_set_resolution (goom->width, goom->height);
237 goom->srcnegotiated = TRUE;
239 return GST_PAD_LINK_OK;
243 gst_goom_src_fixate (GstPad * pad, const GstCaps * caps)
246 GstStructure *structure;
248 if (!gst_caps_is_simple (caps))
251 newcaps = gst_caps_copy (caps);
252 structure = gst_caps_get_structure (newcaps, 0);
254 if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 320)) {
257 if (gst_caps_structure_fixate_field_nearest_int (structure, "height", 240)) {
260 if (gst_caps_structure_fixate_field_nearest_double (structure, "framerate",
265 /* failed to fixate */
266 gst_caps_free (newcaps);
271 gst_goom_chain (GstPad * pad, GstData * _data)
273 GstBuffer *bufin = GST_BUFFER (_data);
280 goom = GST_GOOM (gst_pad_get_parent (pad));
282 GST_DEBUG ("GOOM: chainfunc called");
284 if (GST_IS_EVENT (bufin)) {
285 GstEvent *event = GST_EVENT (bufin);
287 switch (GST_EVENT_TYPE (event)) {
288 case GST_EVENT_DISCONTINUOUS:
292 gst_event_discont_get_value (event, GST_FORMAT_TIME, &value);
294 goom->next_time = value;
297 gst_pad_event_default (pad, event);
303 if (goom->channels == 0) {
304 GST_ELEMENT_ERROR (goom, CORE, NEGOTIATION, (NULL),
305 ("format wasn't negotiated before chain function"));
310 if (!GST_PAD_IS_USABLE (goom->srcpad))
313 samples_in = GST_BUFFER_SIZE (bufin) / (sizeof (gint16) * goom->channels);
315 GST_DEBUG ("input buffer has %d samples", samples_in);
317 if (GST_BUFFER_TIMESTAMP (bufin) < goom->next_time || samples_in < 512) {
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++;
328 for (i = 0; i < 512; i++) {
329 goom->datain[0][i] = *data;
330 goom->datain[1][i] = *data++;
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);
340 goom->next_time += GST_SECOND / goom->fps;
342 gst_pad_push (goom->srcpad, GST_DATA (bufout));
345 gst_buffer_unref (bufin);
347 GST_DEBUG ("GOOM: exiting chainfunc");
350 static GstElementStateReturn
351 gst_goom_change_state (GstElement * element)
353 GstGOOM *goom = GST_GOOM (element);
355 switch (GST_STATE_TRANSITION (element)) {
356 case GST_STATE_NULL_TO_READY:
358 case GST_STATE_READY_TO_NULL:
360 case GST_STATE_READY_TO_PAUSED:
362 goom->srcnegotiated = FALSE;
364 case GST_STATE_PAUSED_TO_READY:
371 if (GST_ELEMENT_CLASS (parent_class)->change_state)
372 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
374 return GST_STATE_SUCCESS;
378 plugin_init (GstPlugin * plugin)
380 return gst_element_register (plugin, "goom", GST_RANK_NONE, GST_TYPE_GOOM);
383 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
386 "GOOM visualization filter",
387 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)