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 GST_PAD_TEMPLATE_FACTORY (src_template,
88 GST_VIDEO_RGB_PAD_TEMPLATE_PROPS_32
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 */
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)
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);
115 static GstElementStateReturn
116 gst_goom_change_state (GstElement *element);
118 static void gst_goom_chain (GstPad *pad, GstData *_data);
120 static GstPadLinkReturn gst_goom_sinkconnect (GstPad *pad, GstCaps *caps);
121 static GstPadLinkReturn gst_goom_srcconnect (GstPad *pad, GstCaps *caps);
123 static GstElementClass *parent_class = NULL;
126 gst_goom_get_type (void)
128 static GType type = 0;
131 static const GTypeInfo info = {
132 sizeof (GstGOOMClass),
133 (GBaseInitFunc) gst_goom_base_init,
135 (GClassInitFunc) gst_goom_class_init,
140 (GInstanceInitFunc) gst_goom_init,
142 type = g_type_register_static (GST_TYPE_ELEMENT, "GstGOOM", &info, 0);
148 gst_goom_base_init (GstGOOMClass *klass)
150 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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));
160 gst_goom_class_init(GstGOOMClass *klass)
162 GObjectClass *gobject_class;
163 GstElementClass *gstelement_class;
165 gobject_class = (GObjectClass*) klass;
166 gstelement_class = (GstElementClass*) klass;
168 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
170 gobject_class->dispose = gst_goom_dispose;
172 gstelement_class->change_state = gst_goom_change_state;
176 gst_goom_init (GstGOOM *goom)
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);
186 GST_FLAG_SET (goom, GST_ELEMENT_EVENT_AWARE);
188 gst_pad_set_chain_function (goom->sinkpad, gst_goom_chain);
189 gst_pad_set_link_function (goom->sinkpad, gst_goom_sinkconnect);
191 gst_pad_set_link_function (goom->srcpad, gst_goom_srcconnect);
195 goom->fps = 25.; /* desired frame rate */
197 /* set to something */
202 gst_goom_dispose (GObject *object)
206 G_OBJECT_CLASS (parent_class)->dispose (object);
209 static GstPadLinkReturn
210 gst_goom_sinkconnect (GstPad *pad, GstCaps *caps)
213 goom = GST_GOOM (gst_pad_get_parent (pad));
215 if (!GST_CAPS_IS_FIXED (caps)) {
216 return GST_PAD_LINK_DELAYED;
219 gst_caps_get_int (caps, "channels", &goom->channels);
221 return GST_PAD_LINK_OK;
224 static GstPadLinkReturn
225 gst_goom_srcconnect (GstPad *pad, GstCaps *caps)
228 goom = GST_GOOM (gst_pad_get_parent (pad));
230 if (!GST_CAPS_IS_FIXED (caps)) {
231 return GST_PAD_LINK_DELAYED;
234 if (gst_caps_has_property_typed (caps, "width", GST_PROPS_INT_TYPE)) {
235 gst_caps_get_int (caps, "width", &goom->width);
237 if (gst_caps_has_property_typed (caps, "height", GST_PROPS_INT_TYPE)) {
238 gst_caps_get_int (caps, "height", &goom->height);
240 if (gst_caps_has_property_typed (caps, "framerate", GST_PROPS_FLOAT_TYPE)) {
241 gst_caps_get_float (caps, "framerate", &goom->fps);
244 goom_set_resolution (goom->width, goom->height);
245 goom->srcnegotiated = TRUE;
247 return GST_PAD_LINK_OK;
251 gst_goom_negotiate_default (GstGOOM *goom)
255 caps = GST_CAPS_NEW (
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)
270 if (gst_pad_try_set_caps (goom->srcpad, caps) <= 0) {
274 goom_set_resolution (goom->width, goom->height);
275 goom->srcnegotiated = TRUE;
281 gst_goom_chain (GstPad *pad, GstData *_data)
283 GstBuffer *bufin = GST_BUFFER (_data);
290 goom = GST_GOOM (gst_pad_get_parent (pad));
292 GST_DEBUG ("GOOM: chainfunc called");
294 if (GST_IS_EVENT (bufin)) {
295 GstEvent *event = GST_EVENT (bufin);
297 switch (GST_EVENT_TYPE (event)) {
298 case GST_EVENT_DISCONTINUOUS:
302 gst_event_discont_get_value (event, GST_FORMAT_TIME, &value);
304 goom->next_time = value;
307 gst_pad_event_default (pad, event);
313 if (goom->channels == 0) {
314 gst_element_error (GST_ELEMENT (goom), "sink format not negotiated");
318 if (!GST_PAD_IS_USABLE (goom->srcpad))
321 if (!goom->srcnegotiated) {
322 if (!gst_goom_negotiate_default (goom)) {
323 gst_element_error (GST_ELEMENT (goom), "could not negotiate src format");
328 samples_in = GST_BUFFER_SIZE (bufin) / (sizeof (gint16) * goom->channels);
330 GST_DEBUG ("input buffer has %d samples", samples_in);
332 if (GST_BUFFER_TIMESTAMP (bufin) < goom->next_time || samples_in < 512) {
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++;
344 for (i=0; i < 512; i++) {
345 goom->datain[0][i] = *data;
346 goom->datain[1][i] = *data++;
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);
356 goom->next_time += GST_SECOND / goom->fps;
358 gst_pad_push (goom->srcpad, GST_DATA (bufout));
361 gst_buffer_unref (bufin);
363 GST_DEBUG ("GOOM: exiting chainfunc");
366 static GstElementStateReturn
367 gst_goom_change_state (GstElement *element)
369 GstGOOM *goom = GST_GOOM (element);
371 switch (GST_STATE_TRANSITION (element)) {
372 case GST_STATE_NULL_TO_READY:
374 case GST_STATE_READY_TO_NULL:
376 case GST_STATE_READY_TO_PAUSED:
378 goom->srcnegotiated = FALSE;
381 case GST_STATE_PAUSED_TO_READY:
387 if (GST_ELEMENT_CLASS (parent_class)->change_state)
388 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
390 return GST_STATE_SUCCESS;
394 plugin_init (GstPlugin *plugin)
396 return gst_element_register (plugin, "goom",
397 GST_RANK_NONE, GST_TYPE_GOOM);
404 "GOOM visualization filter",