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,
138 type = g_type_register_static (GST_TYPE_ELEMENT, "GstGOOM", &info, 0);
144 gst_goom_base_init (GstGOOMClass * klass)
146 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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));
156 gst_goom_class_init (GstGOOMClass * klass)
158 GObjectClass *gobject_class;
159 GstElementClass *gstelement_class;
161 gobject_class = (GObjectClass *) klass;
162 gstelement_class = (GstElementClass *) klass;
164 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
166 gobject_class->dispose = gst_goom_dispose;
168 gstelement_class->change_state = gst_goom_change_state;
172 gst_goom_init (GstGOOM * goom)
174 /* create the sink and src pads */
176 gst_pad_new_from_template (gst_static_pad_template_get (&sink_template),
179 gst_pad_new_from_template (gst_static_pad_template_get (&src_template),
181 gst_element_add_pad (GST_ELEMENT (goom), goom->sinkpad);
182 gst_element_add_pad (GST_ELEMENT (goom), goom->srcpad);
184 GST_FLAG_SET (goom, GST_ELEMENT_EVENT_AWARE);
186 gst_pad_set_chain_function (goom->sinkpad, gst_goom_chain);
187 gst_pad_set_link_function (goom->sinkpad, gst_goom_sinkconnect);
189 gst_pad_set_link_function (goom->srcpad, gst_goom_srcconnect);
190 gst_pad_set_fixate_function (goom->srcpad, gst_goom_src_fixate);
194 goom->fps = 25.; /* desired frame rate */
196 /* set to something */
201 gst_goom_dispose (GObject * object)
205 G_OBJECT_CLASS (parent_class)->dispose (object);
208 static GstPadLinkReturn
209 gst_goom_sinkconnect (GstPad * pad, const GstCaps * caps)
212 GstStructure *structure;
214 goom = GST_GOOM (gst_pad_get_parent (pad));
216 structure = gst_caps_get_structure (caps, 0);
218 gst_structure_get_int (structure, "channels", &goom->channels);
220 return GST_PAD_LINK_OK;
223 static GstPadLinkReturn
224 gst_goom_srcconnect (GstPad * pad, const GstCaps * caps)
227 GstStructure *structure;
229 goom = GST_GOOM (gst_pad_get_parent (pad));
231 structure = gst_caps_get_structure (caps, 0);
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);
237 goom_set_resolution (goom->width, goom->height);
238 goom->srcnegotiated = TRUE;
240 return GST_PAD_LINK_OK;
244 gst_goom_src_fixate (GstPad * pad, const GstCaps * caps)
247 GstStructure *structure;
249 if (!gst_caps_is_simple (caps))
252 newcaps = gst_caps_copy (caps);
253 structure = gst_caps_get_structure (newcaps, 0);
255 if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 320)) {
258 if (gst_caps_structure_fixate_field_nearest_int (structure, "height", 240)) {
261 if (gst_caps_structure_fixate_field_nearest_double (structure, "framerate",
266 /* failed to fixate */
267 gst_caps_free (newcaps);
272 gst_goom_chain (GstPad * pad, GstData * _data)
274 GstBuffer *bufin = GST_BUFFER (_data);
281 goom = GST_GOOM (gst_pad_get_parent (pad));
283 GST_DEBUG ("GOOM: chainfunc called");
285 if (GST_IS_EVENT (bufin)) {
286 GstEvent *event = GST_EVENT (bufin);
288 switch (GST_EVENT_TYPE (event)) {
289 case GST_EVENT_DISCONTINUOUS:
293 gst_event_discont_get_value (event, GST_FORMAT_TIME, &value);
295 goom->next_time = value;
298 gst_pad_event_default (pad, event);
304 if (goom->channels == 0) {
305 GST_ELEMENT_ERROR (goom, CORE, NEGOTIATION, (NULL),
306 ("format wasn't negotiated before chain function"));
311 if (!GST_PAD_IS_USABLE (goom->srcpad))
314 samples_in = GST_BUFFER_SIZE (bufin) / (sizeof (gint16) * goom->channels);
316 GST_DEBUG ("input buffer has %d samples", samples_in);
318 if (GST_BUFFER_TIMESTAMP (bufin) < goom->next_time || samples_in < 512) {
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++;
329 for (i = 0; i < 512; i++) {
330 goom->datain[0][i] = *data;
331 goom->datain[1][i] = *data++;
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);
341 goom->next_time += GST_SECOND / goom->fps;
343 gst_pad_push (goom->srcpad, GST_DATA (bufout));
346 gst_buffer_unref (bufin);
348 GST_DEBUG ("GOOM: exiting chainfunc");
351 static GstElementStateReturn
352 gst_goom_change_state (GstElement * element)
354 GstGOOM *goom = GST_GOOM (element);
356 switch (GST_STATE_TRANSITION (element)) {
357 case GST_STATE_NULL_TO_READY:
359 case GST_STATE_READY_TO_NULL:
361 case GST_STATE_READY_TO_PAUSED:
363 goom->srcnegotiated = FALSE;
365 case GST_STATE_PAUSED_TO_READY:
372 if (GST_ELEMENT_CLASS (parent_class)->change_state)
373 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
375 return GST_STATE_SUCCESS;
379 plugin_init (GstPlugin * plugin)
381 return gst_element_register (plugin, "goom", GST_RANK_NONE, GST_TYPE_GOOM);
384 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
387 "GOOM visualization filter",
388 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN)