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;
38 struct _GstMonoscope {
42 GstPad *sinkpad,*srcpad;
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 static GstStaticPadTemplate src_template =
85 GST_STATIC_PAD_TEMPLATE (
89 GST_STATIC_CAPS (GST_VIDEO_RGB_PAD_TEMPLATE_CAPS_32)
92 static GstStaticPadTemplate sink_template =
93 GST_STATIC_PAD_TEMPLATE (
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_sinkconnect (GstPad *pad, const GstCaps *caps);
109 static GstPadLinkReturn
110 gst_monoscope_srcconnect (GstPad *pad, const GstCaps *caps);
112 static GstElementClass *parent_class = NULL;
115 gst_monoscope_get_type (void)
117 static GType type = 0;
120 static const GTypeInfo info = {
121 sizeof (GstMonoscopeClass),
122 (GBaseInitFunc) gst_monoscope_base_init,
124 (GClassInitFunc) gst_monoscope_class_init,
127 sizeof (GstMonoscope),
129 (GInstanceInitFunc) gst_monoscope_init,
131 type = g_type_register_static (GST_TYPE_ELEMENT, "GstMonoscope", &info, 0);
137 gst_monoscope_base_init (GstMonoscopeClass *klass)
139 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
141 gst_element_class_add_pad_template (element_class,
142 gst_static_pad_template_get (&src_template));
143 gst_element_class_add_pad_template (element_class,
144 gst_static_pad_template_get (&sink_template));
145 gst_element_class_set_details (element_class, &gst_monoscope_details);
149 gst_monoscope_class_init(GstMonoscopeClass *klass)
151 GObjectClass *gobject_class;
152 GstElementClass *gstelement_class;
154 gobject_class = (GObjectClass*) klass;
155 gstelement_class = (GstElementClass*) klass;
157 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
161 gst_monoscope_init (GstMonoscope *monoscope)
163 /* create the sink and src pads */
164 monoscope->sinkpad = gst_pad_new_from_template (
165 gst_static_pad_template_get (&sink_template ), "sink");
166 monoscope->srcpad = gst_pad_new_from_template (
167 gst_static_pad_template_get (&src_template ), "src");
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->sinkpad, gst_monoscope_sinkconnect);
173 gst_pad_set_link_function (monoscope->srcpad, gst_monoscope_srcconnect);
175 monoscope->next_time = 0;
177 /* reset the initial video state */
178 monoscope->first_buffer = TRUE;
179 monoscope->width = 256;
180 monoscope->height = 128;
181 monoscope->fps = 25.; /* desired frame rate */
184 static GstPadLinkReturn
185 gst_monoscope_sinkconnect (GstPad *pad, const GstCaps *caps)
187 GstMonoscope *monoscope;
188 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
190 return GST_PAD_LINK_OK;
193 static GstPadLinkReturn
194 gst_monoscope_negotiate (GstMonoscope *monoscope)
198 caps = gst_caps_new_simple ("video/x-raw-rgb",
199 "bpp", G_TYPE_INT, 32,
200 "depth", G_TYPE_INT, 32,
201 "endianness", G_TYPE_INT, G_BIG_ENDIAN,
202 "red_mask", G_TYPE_INT, R_MASK_32,
203 "green_mask", G_TYPE_INT, G_MASK_32,
204 "blue_mask", G_TYPE_INT, B_MASK_32,
205 "width", G_TYPE_INT, monoscope->width,
206 "height", G_TYPE_INT, monoscope->height,
207 "framerate", G_TYPE_DOUBLE, monoscope->fps, NULL);
209 return gst_pad_try_set_caps (monoscope->srcpad, caps);
212 static GstPadLinkReturn
213 gst_monoscope_srcconnect (GstPad *pad, const GstCaps *caps)
215 GstPadLinkReturn ret;
216 GstMonoscope *monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
217 GstStructure *structure;
219 structure = gst_caps_get_structure (caps, 0);
221 gst_structure_get_int (structure, "width", &monoscope->width);
222 gst_structure_get_int (structure, "height", &monoscope->height);
223 gst_structure_get_double (structure, "framerate", &monoscope->fps);
225 if ((ret = gst_monoscope_negotiate (monoscope)) <= 0)
228 return GST_PAD_LINK_DONE;
232 gst_monoscope_chain (GstPad *pad, GstData *_data)
234 GstBuffer *bufin = GST_BUFFER (_data);
235 GstMonoscope *monoscope;
241 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
243 GST_DEBUG ("Monoscope: chainfunc called");
245 samples_in = GST_BUFFER_SIZE (bufin) / sizeof (gint16);
247 GST_DEBUG ("input buffer has %d samples", samples_in);
249 /* FIXME: should really select the first 1024 samples after the timestamp. */
250 if (GST_BUFFER_TIMESTAMP (bufin) < monoscope->next_time || samples_in < 1024) {
251 GST_DEBUG ("timestamp is %" G_GUINT64_FORMAT ": want >= %" G_GUINT64_FORMAT, GST_BUFFER_TIMESTAMP (bufin), monoscope->next_time);
252 gst_buffer_unref (bufin);
256 data = (gint16 *) GST_BUFFER_DATA (bufin);
257 /* FIXME: Select samples in a better way. */
258 for (i=0; i < 512; i++) {
259 monoscope->datain[i] = *data++;
262 if (monoscope->first_buffer) {
263 monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
264 g_assert(monoscope->visstate != 0);
265 GST_DEBUG ("making new pad");
266 if (!GST_PAD_CAPS (monoscope->srcpad)) {
267 if (gst_monoscope_negotiate (monoscope) <= 0) {
268 gst_element_error (GST_ELEMENT (monoscope), "could not set caps");
272 monoscope->first_buffer = FALSE;
275 bufout = gst_buffer_new ();
276 GST_BUFFER_SIZE (bufout) = monoscope->width * monoscope->height * 4;
277 GST_BUFFER_DATA (bufout) = (guchar *) monoscope_update (monoscope->visstate, monoscope->datain);
278 GST_BUFFER_TIMESTAMP (bufout) = monoscope->next_time;
279 GST_BUFFER_FLAG_SET (bufout, GST_BUFFER_DONTFREE);
281 monoscope->next_time += GST_SECOND / monoscope->fps;
283 gst_pad_push (monoscope->srcpad, GST_DATA (bufout));
285 gst_buffer_unref (bufin);
287 GST_DEBUG ("Monoscope: exiting chainfunc");
292 plugin_init (GstPlugin *plugin)
294 return gst_element_register(plugin, "monoscope",
295 GST_RANK_NONE, GST_TYPE_MONOSCOPE);
302 "Monoscope visualization",