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;
41 GstPad *sinkpad,*srcpad;
43 /* the timestamp of the next frame */
45 gint16 datain[2][512];
52 gboolean srcnegotiated;
55 struct _GstGOOMClass {
56 GstElementClass parent_class;
59 GType gst_goom_get_type(void);
62 /* elementfactory information */
63 static GstElementDetails gst_goom_details = {
66 "Takes frames of data and outputs video frames using the GOOM filter",
67 "Wim Taymans <wim.taymans@chello.be>"
70 /* signals and args */
81 static GstStaticPadTemplate src_template =
82 GST_STATIC_PAD_TEMPLATE (
86 GST_STATIC_CAPS ( GST_VIDEO_CAPS_xRGB_HOST_ENDIAN )
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, "
99 "rate = (int) [ 8000, 96000 ], "
100 "channels = (int) [ 1, 2 ]"
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);
110 static GstElementStateReturn
111 gst_goom_change_state (GstElement *element);
113 static void gst_goom_chain (GstPad *pad, GstData *_data);
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);
119 static GstElementClass *parent_class = NULL;
122 gst_goom_get_type (void)
124 static GType type = 0;
127 static const GTypeInfo info = {
128 sizeof (GstGOOMClass),
129 (GBaseInitFunc) gst_goom_base_init,
131 (GClassInitFunc) gst_goom_class_init,
136 (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 */
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);
182 GST_FLAG_SET (goom, GST_ELEMENT_EVENT_AWARE);
184 gst_pad_set_chain_function (goom->sinkpad, gst_goom_chain);
185 gst_pad_set_link_function (goom->sinkpad, gst_goom_sinkconnect);
187 gst_pad_set_link_function (goom->srcpad, gst_goom_srcconnect);
188 gst_pad_set_fixate_function (goom->srcpad, gst_goom_src_fixate);
192 goom->fps = 25.; /* desired frame rate */
194 /* set to something */
199 gst_goom_dispose (GObject *object)
203 G_OBJECT_CLASS (parent_class)->dispose (object);
206 static GstPadLinkReturn
207 gst_goom_sinkconnect (GstPad *pad, const GstCaps *caps)
210 GstStructure *structure;
212 goom = GST_GOOM (gst_pad_get_parent (pad));
214 structure = gst_caps_get_structure (caps, 0);
216 gst_structure_get_int (structure, "channels", &goom->channels);
218 return GST_PAD_LINK_OK;
221 static GstPadLinkReturn
222 gst_goom_srcconnect (GstPad *pad, const GstCaps *caps)
225 GstStructure *structure;
227 goom = GST_GOOM (gst_pad_get_parent (pad));
229 structure = gst_caps_get_structure (caps, 0);
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);
235 goom_set_resolution (goom->width, goom->height);
236 goom->srcnegotiated = TRUE;
238 return GST_PAD_LINK_OK;
242 gst_goom_src_fixate (GstPad *pad, const GstCaps *caps)
245 GstStructure *structure;
247 if (!gst_caps_is_simple (caps)) return NULL;
249 newcaps = gst_caps_copy (caps);
250 structure = gst_caps_get_structure (newcaps, 0);
252 if (gst_caps_structure_fixate_field_nearest_int (structure, "width", 320)) {
255 if (gst_caps_structure_fixate_field_nearest_int (structure, "height", 240)) {
258 if (gst_caps_structure_fixate_field_nearest_double (structure, "framerate",
263 /* failed to fixate */
264 gst_caps_free (newcaps);
269 gst_goom_chain (GstPad *pad, GstData *_data)
271 GstBuffer *bufin = GST_BUFFER (_data);
278 goom = GST_GOOM (gst_pad_get_parent (pad));
280 GST_DEBUG ("GOOM: chainfunc called");
282 if (GST_IS_EVENT (bufin)) {
283 GstEvent *event = GST_EVENT (bufin);
285 switch (GST_EVENT_TYPE (event)) {
286 case GST_EVENT_DISCONTINUOUS:
290 gst_event_discont_get_value (event, GST_FORMAT_TIME, &value);
292 goom->next_time = value;
295 gst_pad_event_default (pad, event);
301 if (goom->channels == 0) {
302 GST_ELEMENT_ERROR (goom, CORE, NEGOTIATION, (NULL),
303 ("format wasn't negotiated before chain function"));
308 if (!GST_PAD_IS_USABLE (goom->srcpad))
311 samples_in = GST_BUFFER_SIZE (bufin) / (sizeof (gint16) * goom->channels);
313 GST_DEBUG ("input buffer has %d samples", samples_in);
315 if (GST_BUFFER_TIMESTAMP (bufin) < goom->next_time || samples_in < 512) {
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++;
327 for (i=0; i < 512; i++) {
328 goom->datain[0][i] = *data;
329 goom->datain[1][i] = *data++;
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);
339 goom->next_time += GST_SECOND / goom->fps;
341 gst_pad_push (goom->srcpad, GST_DATA (bufout));
344 gst_buffer_unref (bufin);
346 GST_DEBUG ("GOOM: exiting chainfunc");
349 static GstElementStateReturn
350 gst_goom_change_state (GstElement *element)
352 GstGOOM *goom = GST_GOOM (element);
354 switch (GST_STATE_TRANSITION (element)) {
355 case GST_STATE_NULL_TO_READY:
357 case GST_STATE_READY_TO_NULL:
359 case GST_STATE_READY_TO_PAUSED:
361 goom->srcnegotiated = FALSE;
363 case GST_STATE_PAUSED_TO_READY:
370 if (GST_ELEMENT_CLASS (parent_class)->change_state)
371 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
373 return GST_STATE_SUCCESS;
377 plugin_init (GstPlugin *plugin)
379 return gst_element_register (plugin, "goom",
380 GST_RANK_NONE, GST_TYPE_GOOM);
387 "GOOM visualization filter",