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 <gst/audio/audio.h>
27 #include "monoscope.h"
29 #define GST_TYPE_MONOSCOPE (gst_monoscope_get_type())
30 #define GST_MONOSCOPE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MONOSCOPE,GstMonoscope))
31 #define GST_MONOSCOPE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MONOSCOPE,GstMonoscope))
32 #define GST_IS_MONOSCOPE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MONOSCOPE))
33 #define GST_IS_MONOSCOPE_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MONOSCOPE))
35 typedef struct _GstMonoscope GstMonoscope;
36 typedef struct _GstMonoscopeClass GstMonoscopeClass;
43 GstPad *sinkpad, *srcpad;
45 /* the timestamp of the next frame */
53 gboolean first_buffer;
55 /* visualisation state */
56 struct monoscope_state *visstate;
59 struct _GstMonoscopeClass
61 GstElementClass parent_class;
64 GType gst_monoscope_get_type (void);
67 /* elementfactory information */
68 static GstElementDetails gst_monoscope_details = {
71 "Displays a highly stabilised waveform of audio input",
72 "Richard Boulton <richard@tartarus.org>"
75 /* signals and args */
88 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
91 GST_STATIC_CAPS (GST_VIDEO_CAPS_xRGB_HOST_ENDIAN)
94 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
97 GST_STATIC_CAPS (GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_CAPS)
101 static void gst_monoscope_class_init (GstMonoscopeClass * klass);
102 static void gst_monoscope_base_init (GstMonoscopeClass * klass);
103 static void gst_monoscope_init (GstMonoscope * monoscope);
105 static void gst_monoscope_chain (GstPad * pad, GstData * _data);
107 static GstPadLinkReturn
108 gst_monoscope_srcconnect (GstPad * pad, const GstCaps * caps);
110 static GstElementClass *parent_class = NULL;
113 gst_monoscope_get_type (void)
115 static GType type = 0;
118 static const GTypeInfo info = {
119 sizeof (GstMonoscopeClass),
120 (GBaseInitFunc) gst_monoscope_base_init,
122 (GClassInitFunc) gst_monoscope_class_init,
125 sizeof (GstMonoscope),
127 (GInstanceInitFunc) gst_monoscope_init,
129 type = g_type_register_static (GST_TYPE_ELEMENT, "GstMonoscope", &info, 0);
135 gst_monoscope_base_init (GstMonoscopeClass * klass)
137 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
139 gst_element_class_add_pad_template (element_class,
140 gst_static_pad_template_get (&src_template));
141 gst_element_class_add_pad_template (element_class,
142 gst_static_pad_template_get (&sink_template));
143 gst_element_class_set_details (element_class, &gst_monoscope_details);
147 gst_monoscope_class_init (GstMonoscopeClass * klass)
149 GObjectClass *gobject_class;
150 GstElementClass *gstelement_class;
152 gobject_class = (GObjectClass *) klass;
153 gstelement_class = (GstElementClass *) klass;
155 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
159 gst_monoscope_init (GstMonoscope * monoscope)
161 /* create the sink and src pads */
163 gst_pad_new_from_template (gst_static_pad_template_get (&sink_template),
166 gst_pad_new_from_template (gst_static_pad_template_get (&src_template),
168 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
169 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
171 gst_pad_set_chain_function (monoscope->sinkpad, gst_monoscope_chain);
172 gst_pad_set_link_function (monoscope->srcpad, gst_monoscope_srcconnect);
174 monoscope->next_time = 0;
176 /* reset the initial video state */
177 monoscope->first_buffer = TRUE;
178 monoscope->width = 256;
179 monoscope->height = 128;
180 monoscope->fps = 25.; /* desired frame rate */
183 static GstPadLinkReturn
184 gst_monoscope_srcconnect (GstPad * pad, const GstCaps * caps)
186 GstMonoscope *monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
187 GstStructure *structure;
189 structure = gst_caps_get_structure (caps, 0);
191 gst_structure_get_int (structure, "width", &monoscope->width);
192 gst_structure_get_int (structure, "height", &monoscope->height);
193 gst_structure_get_double (structure, "framerate", &monoscope->fps);
195 return GST_PAD_LINK_OK;
199 gst_monoscope_chain (GstPad * pad, GstData * _data)
201 GstBuffer *bufin = GST_BUFFER (_data);
202 GstMonoscope *monoscope;
208 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
210 GST_DEBUG ("Monoscope: chainfunc called");
212 samples_in = GST_BUFFER_SIZE (bufin) / sizeof (gint16);
214 GST_DEBUG ("input buffer has %d samples", samples_in);
216 /* FIXME: should really select the first 1024 samples after the timestamp. */
217 if (GST_BUFFER_TIMESTAMP (bufin) < monoscope->next_time || samples_in < 1024) {
218 GST_DEBUG ("timestamp is %" G_GUINT64_FORMAT ": want >= %" G_GUINT64_FORMAT,
219 GST_BUFFER_TIMESTAMP (bufin), monoscope->next_time);
220 gst_buffer_unref (bufin);
224 data = (gint16 *) GST_BUFFER_DATA (bufin);
225 /* FIXME: Select samples in a better way. */
226 for (i = 0; i < 512; i++) {
227 monoscope->datain[i] = *data++;
230 if (monoscope->first_buffer) {
231 monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
232 g_assert (monoscope->visstate != 0);
233 GST_DEBUG ("making new pad");
234 if (!gst_pad_is_negotiated (monoscope->srcpad)) {
235 if (gst_pad_renegotiate (monoscope->srcpad) <= 0) {
236 GST_ELEMENT_ERROR (monoscope, CORE, NEGOTIATION, (NULL), (NULL));
240 monoscope->first_buffer = FALSE;
243 bufout = gst_buffer_new ();
244 GST_BUFFER_SIZE (bufout) = monoscope->width * monoscope->height * 4;
245 GST_BUFFER_DATA (bufout) =
246 (guchar *) monoscope_update (monoscope->visstate, monoscope->datain);
247 GST_BUFFER_TIMESTAMP (bufout) = monoscope->next_time;
248 GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
250 monoscope->next_time += GST_SECOND / monoscope->fps;
252 gst_pad_push (monoscope->srcpad, GST_DATA (bufout));
254 gst_buffer_unref (bufin);
256 GST_DEBUG ("Monoscope: exiting chainfunc");
261 plugin_init (GstPlugin * plugin)
263 return gst_element_register (plugin, "monoscope",
264 GST_RANK_NONE, GST_TYPE_MONOSCOPE);
267 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
270 "Monoscope visualization",
271 plugin_init, VERSION, "LGPL", GST_PACKAGE, GST_ORIGIN)