1 /* gstmonoscope.c: implementation of monoscope drawing element
2 * Copyright (C) <2002> 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 "monoscope.h"
28 #define GST_TYPE_MONOSCOPE (gst_monoscope_get_type())
29 #define GST_MONOSCOPE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MONOSCOPE,GstMonoscope))
30 #define GST_MONOSCOPE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MONOSCOPE,GstMonoscope))
31 #define GST_IS_MONOSCOPE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MONOSCOPE))
32 #define GST_IS_MONOSCOPE_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MONOSCOPE))
34 typedef struct _GstMonoscope GstMonoscope;
35 typedef struct _GstMonoscopeClass GstMonoscopeClass;
37 struct _GstMonoscope {
41 GstPad *sinkpad,*srcpad;
42 GstBufferPool *peerpool;
44 /* the timestamp of the next frame */
52 gboolean first_buffer;
54 /* visualisation state */
55 struct monoscope_state * visstate;
58 struct _GstMonoscopeClass {
59 GstElementClass parent_class;
62 GType gst_monoscope_get_type(void);
65 /* elementfactory information */
66 static GstElementDetails gst_monoscope_details = {
70 "Displays a highly stabilised waveform of audio input",
72 "Richard Boulton <richard@tartarus.org>",
76 /* signals and args */
87 GST_PAD_TEMPLATE_FACTORY (src_template,
94 "bpp", GST_PROPS_INT (32),
95 "depth", GST_PROPS_INT (32),
96 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
97 "red_mask", GST_PROPS_INT (R_MASK_32),
98 "green_mask", GST_PROPS_INT (G_MASK_32),
99 "blue_mask", GST_PROPS_INT (B_MASK_32),
100 "width", GST_PROPS_INT_RANGE (16, 4096),
101 "height", GST_PROPS_INT_RANGE (16, 4096),
102 "framerate", GST_PROPS_FLOAT_RANGE (0, G_MAXFLOAT)
106 GST_PAD_TEMPLATE_FACTORY (sink_template,
107 "sink", /* the name of the pads */
108 GST_PAD_SINK, /* type of the pad */
109 GST_PAD_ALWAYS, /* ALWAYS/SOMETIMES */
111 "monoscopesink", /* the name of the caps */
112 "audio/x-raw-int", /* the mime type of the caps */
113 /* Properties follow: */
114 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
115 "signed", GST_PROPS_BOOLEAN (TRUE),
116 "width", GST_PROPS_INT (16),
117 "depth", GST_PROPS_INT (16),
118 "rate", GST_PROPS_INT_RANGE (8000, 96000),
119 "channels", GST_PROPS_INT (1)
124 static void gst_monoscope_class_init (GstMonoscopeClass *klass);
125 static void gst_monoscope_init (GstMonoscope *monoscope);
127 static void gst_monoscope_chain (GstPad *pad, GstBuffer *buf);
129 static GstPadLinkReturn
130 gst_monoscope_sinkconnect (GstPad *pad, GstCaps *caps);
131 static GstPadLinkReturn
132 gst_monoscope_srcconnect (GstPad *pad, GstCaps *caps);
134 static GstElementClass *parent_class = NULL;
137 gst_monoscope_get_type (void)
139 static GType type = 0;
142 static const GTypeInfo info = {
143 sizeof (GstMonoscopeClass),
146 (GClassInitFunc) gst_monoscope_class_init,
149 sizeof (GstMonoscope),
151 (GInstanceInitFunc) gst_monoscope_init,
153 type = g_type_register_static (GST_TYPE_ELEMENT, "GstMonoscope", &info, 0);
159 gst_monoscope_class_init(GstMonoscopeClass *klass)
161 GObjectClass *gobject_class;
162 GstElementClass *gstelement_class;
164 gobject_class = (GObjectClass*) klass;
165 gstelement_class = (GstElementClass*) klass;
167 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
171 gst_monoscope_init (GstMonoscope *monoscope)
173 /* create the sink and src pads */
174 monoscope->sinkpad = gst_pad_new_from_template (
175 GST_PAD_TEMPLATE_GET (sink_template ), "sink");
176 monoscope->srcpad = gst_pad_new_from_template (
177 GST_PAD_TEMPLATE_GET (src_template ), "src");
178 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
179 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
181 gst_pad_set_chain_function (monoscope->sinkpad, gst_monoscope_chain);
182 gst_pad_set_link_function (monoscope->sinkpad, gst_monoscope_sinkconnect);
183 gst_pad_set_link_function (monoscope->srcpad, gst_monoscope_srcconnect);
185 monoscope->next_time = 0;
186 monoscope->peerpool = NULL;
188 /* reset the initial video state */
189 monoscope->first_buffer = TRUE;
190 monoscope->width = 256;
191 monoscope->height = 128;
192 monoscope->fps = 25.; /* desired frame rate */
195 static GstPadLinkReturn
196 gst_monoscope_sinkconnect (GstPad *pad, GstCaps *caps)
198 GstMonoscope *monoscope;
199 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
201 if (!GST_CAPS_IS_FIXED (caps)) {
202 return GST_PAD_LINK_DELAYED;
205 return GST_PAD_LINK_OK;
208 static GstPadLinkReturn
209 gst_monoscope_negotiate (GstMonoscope *monoscope)
213 caps = GST_CAPS_NEW (
216 "bpp", GST_PROPS_INT (32),
217 "depth", GST_PROPS_INT (32),
218 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
219 "red_mask", GST_PROPS_INT (R_MASK_32),
220 "green_mask", GST_PROPS_INT (G_MASK_32),
221 "blue_mask", GST_PROPS_INT (B_MASK_32),
222 "width", GST_PROPS_INT (monoscope->width),
223 "height", GST_PROPS_INT (monoscope->height),
224 "framerate", GST_PROPS_FLOAT (monoscope->fps)
227 return gst_pad_try_set_caps (monoscope->srcpad, caps);
230 static GstPadLinkReturn
231 gst_monoscope_srcconnect (GstPad *pad, GstCaps *caps)
233 GstPadLinkReturn ret;
234 GstMonoscope *monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
236 if (gst_caps_has_property_typed (caps, "width", GST_PROPS_INT_TYPE)) {
237 gst_caps_get_int (caps, "width", &monoscope->width);
239 if (gst_caps_has_property_typed (caps, "height", GST_PROPS_INT_TYPE)) {
240 gst_caps_get_int (caps, "height", &monoscope->height);
242 if (gst_caps_has_property_typed (caps, "framerate", GST_PROPS_FLOAT_TYPE)) {
243 gst_caps_get_float (caps, "framerate", &monoscope->fps);
246 if ((ret = gst_monoscope_negotiate (monoscope)) <= 0)
249 return GST_PAD_LINK_DONE;
253 gst_monoscope_chain (GstPad *pad, GstBuffer *bufin)
255 GstMonoscope *monoscope;
261 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
263 GST_DEBUG ("Monoscope: chainfunc called");
265 samples_in = GST_BUFFER_SIZE (bufin) / sizeof (gint16);
267 GST_DEBUG ("input buffer has %d samples", samples_in);
269 /* FIXME: should really select the first 1024 samples after the timestamp. */
270 if (GST_BUFFER_TIMESTAMP (bufin) < monoscope->next_time || samples_in < 1024) {
271 GST_DEBUG ("timestamp is %" G_GUINT64_FORMAT ": want >= %" G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (bufin), monoscope->next_time);
272 gst_buffer_unref (bufin);
276 data = (gint16 *) GST_BUFFER_DATA (bufin);
277 /* FIXME: Select samples in a better way. */
278 for (i=0; i < 512; i++) {
279 monoscope->datain[i] = *data++;
282 if (monoscope->first_buffer) {
283 monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
284 g_assert(monoscope->visstate != 0);
285 GST_DEBUG ("making new pad");
286 if (!GST_PAD_CAPS (monoscope->srcpad)) {
287 if (gst_monoscope_negotiate (monoscope) <= 0) {
288 gst_element_error (GST_ELEMENT (monoscope), "could not set caps");
292 monoscope->first_buffer = FALSE;
295 bufout = gst_buffer_new ();
296 GST_BUFFER_SIZE (bufout) = monoscope->width * monoscope->height * 4;
297 GST_BUFFER_DATA (bufout) = (guchar *) monoscope_update (monoscope->visstate, monoscope->datain);
298 GST_BUFFER_TIMESTAMP (bufout) = monoscope->next_time;
299 GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
301 monoscope->next_time += GST_SECOND / monoscope->fps;
303 gst_pad_push (monoscope->srcpad, bufout);
305 gst_buffer_unref (bufin);
307 GST_DEBUG ("Monoscope: exiting chainfunc");
312 plugin_init (GModule *module, GstPlugin *plugin)
314 GstElementFactory *factory;
316 /* create an elementfactory for the monoscope element */
317 factory = gst_element_factory_new("monoscope",GST_TYPE_MONOSCOPE,
318 &gst_monoscope_details);
319 g_return_val_if_fail(factory != NULL, FALSE);
321 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (src_template));
322 gst_element_factory_add_pad_template (factory, GST_PAD_TEMPLATE_GET (sink_template));
324 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
329 GstPluginDesc plugin_desc = {