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 = {
69 "Displays a highly stabilised waveform of audio input",
70 "Richard Boulton <richard@tartarus.org>"
73 /* signals and args */
84 GST_PAD_TEMPLATE_FACTORY (src_template,
91 "bpp", GST_PROPS_INT (32),
92 "depth", GST_PROPS_INT (32),
93 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
94 "red_mask", GST_PROPS_INT (R_MASK_32),
95 "green_mask", GST_PROPS_INT (G_MASK_32),
96 "blue_mask", GST_PROPS_INT (B_MASK_32),
97 "width", GST_PROPS_INT_RANGE (16, 4096),
98 "height", GST_PROPS_INT_RANGE (16, 4096),
99 "framerate", GST_PROPS_FLOAT_RANGE (0, G_MAXFLOAT)
103 GST_PAD_TEMPLATE_FACTORY (sink_template,
104 "sink", /* the name of the pads */
105 GST_PAD_SINK, /* type of the pad */
106 GST_PAD_ALWAYS, /* ALWAYS/SOMETIMES */
108 "monoscopesink", /* the name of the caps */
109 "audio/x-raw-int", /* the mime type of the caps */
110 /* Properties follow: */
111 "endianness", GST_PROPS_INT (G_BYTE_ORDER),
112 "signed", GST_PROPS_BOOLEAN (TRUE),
113 "width", GST_PROPS_INT (16),
114 "depth", GST_PROPS_INT (16),
115 "rate", GST_PROPS_INT_RANGE (8000, 96000),
116 "channels", GST_PROPS_INT (1)
121 static void gst_monoscope_class_init (GstMonoscopeClass *klass);
122 static void gst_monoscope_base_init (GstMonoscopeClass *klass);
123 static void gst_monoscope_init (GstMonoscope *monoscope);
125 static void gst_monoscope_chain (GstPad *pad, GstData *_data);
127 static GstPadLinkReturn
128 gst_monoscope_sinkconnect (GstPad *pad, GstCaps *caps);
129 static GstPadLinkReturn
130 gst_monoscope_srcconnect (GstPad *pad, GstCaps *caps);
132 static GstElementClass *parent_class = NULL;
135 gst_monoscope_get_type (void)
137 static GType type = 0;
140 static const GTypeInfo info = {
141 sizeof (GstMonoscopeClass),
142 (GBaseInitFunc) gst_monoscope_base_init,
144 (GClassInitFunc) gst_monoscope_class_init,
147 sizeof (GstMonoscope),
149 (GInstanceInitFunc) gst_monoscope_init,
151 type = g_type_register_static (GST_TYPE_ELEMENT, "GstMonoscope", &info, 0);
157 gst_monoscope_base_init (GstMonoscopeClass *klass)
159 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
161 gst_element_class_add_pad_template (element_class,
162 GST_PAD_TEMPLATE_GET (src_template));
163 gst_element_class_add_pad_template (element_class,
164 GST_PAD_TEMPLATE_GET (sink_template));
165 gst_element_class_set_details (element_class, &gst_monoscope_details);
169 gst_monoscope_class_init(GstMonoscopeClass *klass)
171 GObjectClass *gobject_class;
172 GstElementClass *gstelement_class;
174 gobject_class = (GObjectClass*) klass;
175 gstelement_class = (GstElementClass*) klass;
177 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
181 gst_monoscope_init (GstMonoscope *monoscope)
183 /* create the sink and src pads */
184 monoscope->sinkpad = gst_pad_new_from_template (
185 GST_PAD_TEMPLATE_GET (sink_template ), "sink");
186 monoscope->srcpad = gst_pad_new_from_template (
187 GST_PAD_TEMPLATE_GET (src_template ), "src");
188 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
189 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
191 gst_pad_set_chain_function (monoscope->sinkpad, gst_monoscope_chain);
192 gst_pad_set_link_function (monoscope->sinkpad, gst_monoscope_sinkconnect);
193 gst_pad_set_link_function (monoscope->srcpad, gst_monoscope_srcconnect);
195 monoscope->next_time = 0;
196 monoscope->peerpool = NULL;
198 /* reset the initial video state */
199 monoscope->first_buffer = TRUE;
200 monoscope->width = 256;
201 monoscope->height = 128;
202 monoscope->fps = 25.; /* desired frame rate */
205 static GstPadLinkReturn
206 gst_monoscope_sinkconnect (GstPad *pad, GstCaps *caps)
208 GstMonoscope *monoscope;
209 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
211 if (!GST_CAPS_IS_FIXED (caps)) {
212 return GST_PAD_LINK_DELAYED;
215 return GST_PAD_LINK_OK;
218 static GstPadLinkReturn
219 gst_monoscope_negotiate (GstMonoscope *monoscope)
223 caps = GST_CAPS_NEW (
226 "bpp", GST_PROPS_INT (32),
227 "depth", GST_PROPS_INT (32),
228 "endianness", GST_PROPS_INT (G_BIG_ENDIAN),
229 "red_mask", GST_PROPS_INT (R_MASK_32),
230 "green_mask", GST_PROPS_INT (G_MASK_32),
231 "blue_mask", GST_PROPS_INT (B_MASK_32),
232 "width", GST_PROPS_INT (monoscope->width),
233 "height", GST_PROPS_INT (monoscope->height),
234 "framerate", GST_PROPS_FLOAT (monoscope->fps)
237 return gst_pad_try_set_caps (monoscope->srcpad, caps);
240 static GstPadLinkReturn
241 gst_monoscope_srcconnect (GstPad *pad, GstCaps *caps)
243 GstPadLinkReturn ret;
244 GstMonoscope *monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
246 if (gst_caps_has_property_typed (caps, "width", GST_PROPS_INT_TYPE)) {
247 gst_caps_get_int (caps, "width", &monoscope->width);
249 if (gst_caps_has_property_typed (caps, "height", GST_PROPS_INT_TYPE)) {
250 gst_caps_get_int (caps, "height", &monoscope->height);
252 if (gst_caps_has_property_typed (caps, "framerate", GST_PROPS_FLOAT_TYPE)) {
253 gst_caps_get_float (caps, "framerate", &monoscope->fps);
256 if ((ret = gst_monoscope_negotiate (monoscope)) <= 0)
259 return GST_PAD_LINK_DONE;
263 gst_monoscope_chain (GstPad *pad, GstData *_data)
265 GstBuffer *bufin = GST_BUFFER (_data);
266 GstMonoscope *monoscope;
272 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
274 GST_DEBUG ("Monoscope: chainfunc called");
276 samples_in = GST_BUFFER_SIZE (bufin) / sizeof (gint16);
278 GST_DEBUG ("input buffer has %d samples", samples_in);
280 /* FIXME: should really select the first 1024 samples after the timestamp. */
281 if (GST_BUFFER_TIMESTAMP (bufin) < monoscope->next_time || samples_in < 1024) {
282 GST_DEBUG ("timestamp is %" G_GUINT64_FORMAT ": want >= %" G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (bufin), monoscope->next_time);
283 gst_buffer_unref (bufin);
287 data = (gint16 *) GST_BUFFER_DATA (bufin);
288 /* FIXME: Select samples in a better way. */
289 for (i=0; i < 512; i++) {
290 monoscope->datain[i] = *data++;
293 if (monoscope->first_buffer) {
294 monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
295 g_assert(monoscope->visstate != 0);
296 GST_DEBUG ("making new pad");
297 if (!GST_PAD_CAPS (monoscope->srcpad)) {
298 if (gst_monoscope_negotiate (monoscope) <= 0) {
299 gst_element_error (GST_ELEMENT (monoscope), "could not set caps");
303 monoscope->first_buffer = FALSE;
306 bufout = gst_buffer_new ();
307 GST_BUFFER_SIZE (bufout) = monoscope->width * monoscope->height * 4;
308 GST_BUFFER_DATA (bufout) = (guchar *) monoscope_update (monoscope->visstate, monoscope->datain);
309 GST_BUFFER_TIMESTAMP (bufout) = monoscope->next_time;
310 GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
312 monoscope->next_time += GST_SECOND / monoscope->fps;
314 gst_pad_push (monoscope->srcpad, GST_DATA (bufout));
316 gst_buffer_unref (bufin);
318 GST_DEBUG ("Monoscope: exiting chainfunc");
323 plugin_init (GstPlugin *plugin)
325 return gst_element_register(plugin, "monoscope",
326 GST_RANK_NONE, GST_TYPE_MONOSCOPE);
333 "Monoscope visualization",