1 /* gstmonoscope.c: implementation of monoscope drawing element
2 * Copyright (C) <2002> Richard Boulton <richard@tartarus.org>
3 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) <2006> Wim Taymans <wim at fluendo dot com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-monoscope
26 * Monoscope is an audio visualisation element. It creates a coloured
27 * curve of the audio signal like on an oscilloscope.
30 * <title>Example launch line</title>
32 * gst-launch -v audiotestsrc ! audioconvert ! monoscope ! ffmpegcolorspace ! ximagesink
41 #include <gst/video/video.h>
42 #include <gst/audio/audio.h>
44 #include "gstmonoscope.h"
45 #include "monoscope.h"
47 GST_DEBUG_CATEGORY_STATIC (monoscope_debug);
48 #define GST_CAT_DEFAULT monoscope_debug
50 #if G_BYTE_ORDER == G_BIG_ENDIAN
51 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
54 GST_STATIC_CAPS ("video/x-raw-rgb, "
57 "endianness = (int) BIG_ENDIAN, "
58 "red_mask = (int) " GST_VIDEO_BYTE2_MASK_32 ", "
59 "green_mask = (int) " GST_VIDEO_BYTE3_MASK_32 ", "
60 "blue_mask = (int) " GST_VIDEO_BYTE4_MASK_32 ", "
62 "height = (int)128, " "framerate = " GST_VIDEO_FPS_RANGE)
65 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
68 GST_STATIC_CAPS ("video/x-raw-rgb, "
71 "endianness = (int) BIG_ENDIAN, "
72 "red_mask = (int) " GST_VIDEO_BYTE3_MASK_32 ", "
73 "green_mask = (int) " GST_VIDEO_BYTE2_MASK_32 ", "
74 "blue_mask = (int) " GST_VIDEO_BYTE1_MASK_32 ", "
76 "height = (int)128, " "framerate = " GST_VIDEO_FPS_RANGE)
80 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
83 GST_STATIC_CAPS (GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_CAPS)
87 GST_BOILERPLATE (GstMonoscope, gst_monoscope, GstElement, GST_TYPE_ELEMENT);
89 static void gst_monoscope_finalize (GObject * object);
90 static GstFlowReturn gst_monoscope_chain (GstPad * pad, GstBuffer * buf);
91 static gboolean gst_monoscope_src_setcaps (GstPad * pad, GstCaps * caps);
92 static gboolean gst_monoscope_sink_setcaps (GstPad * pad, GstCaps * caps);
93 static void gst_monoscope_reset (GstMonoscope * monoscope);
94 static gboolean gst_monoscope_sink_event (GstPad * pad, GstEvent * event);
95 static gboolean gst_monoscope_src_event (GstPad * pad, GstEvent * event);
96 static GstStateChangeReturn gst_monoscope_change_state (GstElement * element,
97 GstStateChange transition);
100 gst_monoscope_base_init (gpointer klass)
102 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
104 gst_element_class_add_static_pad_template (element_class, &src_template);
105 gst_element_class_add_static_pad_template (element_class,
107 gst_element_class_set_details_simple (element_class, "Monoscope",
109 "Displays a highly stabilised waveform of audio input",
110 "Richard Boulton <richard@tartarus.org>");
114 gst_monoscope_class_init (GstMonoscopeClass * klass)
116 GObjectClass *gobject_class;
117 GstElementClass *gstelement_class;
119 gobject_class = (GObjectClass *) klass;
120 gstelement_class = (GstElementClass *) klass;
122 gobject_class->finalize = gst_monoscope_finalize;
124 gstelement_class->change_state =
125 GST_DEBUG_FUNCPTR (gst_monoscope_change_state);
129 gst_monoscope_init (GstMonoscope * monoscope, GstMonoscopeClass * klass)
132 gst_pad_new_from_static_template (&sink_template, "sink");
133 gst_pad_set_chain_function (monoscope->sinkpad,
134 GST_DEBUG_FUNCPTR (gst_monoscope_chain));
135 gst_pad_set_event_function (monoscope->sinkpad,
136 GST_DEBUG_FUNCPTR (gst_monoscope_sink_event));
137 gst_pad_set_setcaps_function (monoscope->sinkpad,
138 GST_DEBUG_FUNCPTR (gst_monoscope_sink_setcaps));
139 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
141 monoscope->srcpad = gst_pad_new_from_static_template (&src_template, "src");
142 gst_pad_set_setcaps_function (monoscope->srcpad,
143 GST_DEBUG_FUNCPTR (gst_monoscope_src_setcaps));
144 gst_pad_set_event_function (monoscope->srcpad,
145 GST_DEBUG_FUNCPTR (gst_monoscope_src_event));
146 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
148 monoscope->adapter = gst_adapter_new ();
149 monoscope->next_ts = GST_CLOCK_TIME_NONE;
150 monoscope->bps = sizeof (gint16);
152 /* reset the initial video state */
153 monoscope->width = 256;
154 monoscope->height = 128;
155 monoscope->fps_num = 25; /* desired frame rate */
156 monoscope->fps_denom = 1;
157 monoscope->visstate = NULL;
159 /* reset the initial audio state */
160 monoscope->rate = GST_AUDIO_DEF_RATE;
164 gst_monoscope_finalize (GObject * object)
166 GstMonoscope *monoscope = GST_MONOSCOPE (object);
168 if (monoscope->visstate)
169 monoscope_close (monoscope->visstate);
171 g_object_unref (monoscope->adapter);
173 G_OBJECT_CLASS (parent_class)->finalize (object);
177 gst_monoscope_reset (GstMonoscope * monoscope)
179 monoscope->next_ts = GST_CLOCK_TIME_NONE;
181 gst_adapter_clear (monoscope->adapter);
182 gst_segment_init (&monoscope->segment, GST_FORMAT_UNDEFINED);
184 GST_OBJECT_LOCK (monoscope);
185 monoscope->proportion = 1.0;
186 monoscope->earliest_time = -1;
187 GST_OBJECT_UNLOCK (monoscope);
191 gst_monoscope_sink_setcaps (GstPad * pad, GstCaps * caps)
193 GstMonoscope *monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
194 GstStructure *structure;
196 structure = gst_caps_get_structure (caps, 0);
198 gst_structure_get_int (structure, "rate", &monoscope->rate);
200 GST_DEBUG_OBJECT (monoscope, "sample rate = %d", monoscope->rate);
205 gst_monoscope_src_setcaps (GstPad * pad, GstCaps * caps)
207 GstMonoscope *monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
208 GstStructure *structure;
210 structure = gst_caps_get_structure (caps, 0);
212 gst_structure_get_int (structure, "width", &monoscope->width);
213 gst_structure_get_int (structure, "height", &monoscope->height);
214 gst_structure_get_fraction (structure, "framerate", &monoscope->fps_num,
215 &monoscope->fps_denom);
217 monoscope->outsize = monoscope->width * monoscope->height * 4;
218 monoscope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
219 monoscope->fps_denom, monoscope->fps_num);
221 gst_util_uint64_scale_int (monoscope->rate, monoscope->fps_denom,
224 GST_DEBUG_OBJECT (monoscope, "dimension %dx%d, framerate %d/%d, spf %d",
225 monoscope->width, monoscope->height, monoscope->fps_num,
226 monoscope->fps_denom, monoscope->spf);
228 if (monoscope->visstate) {
229 monoscope_close (monoscope->visstate);
230 monoscope->visstate = NULL;
233 monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
235 return (monoscope->visstate != NULL);
239 gst_monoscope_src_negotiate (GstMonoscope * monoscope)
241 GstCaps *othercaps, *target;
242 GstStructure *structure;
243 const GstCaps *templ;
245 templ = gst_pad_get_pad_template_caps (monoscope->srcpad);
247 GST_DEBUG_OBJECT (monoscope, "performing negotiation");
249 /* see what the peer can do */
250 othercaps = gst_pad_peer_get_caps (monoscope->srcpad);
252 target = gst_caps_intersect (othercaps, templ);
253 gst_caps_unref (othercaps);
255 if (gst_caps_is_empty (target))
258 gst_caps_truncate (target);
260 target = gst_caps_ref ((GstCaps *) templ);
263 structure = gst_caps_get_structure (target, 0);
264 gst_structure_fixate_field_nearest_int (structure, "width", 320);
265 gst_structure_fixate_field_nearest_int (structure, "height", 240);
266 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
268 gst_pad_set_caps (monoscope->srcpad, target);
269 gst_caps_unref (target);
275 gst_caps_unref (target);
281 get_buffer (GstMonoscope * monoscope, GstBuffer ** outbuf)
285 if (GST_PAD_CAPS (monoscope->srcpad) == NULL) {
286 if (!gst_monoscope_src_negotiate (monoscope))
287 return GST_FLOW_NOT_NEGOTIATED;
290 GST_LOG_OBJECT (monoscope, "allocating output buffer of size %d with caps %"
291 GST_PTR_FORMAT, monoscope->outsize, GST_PAD_CAPS (monoscope->srcpad));
294 gst_pad_alloc_buffer_and_set_caps (monoscope->srcpad,
295 GST_BUFFER_OFFSET_NONE, monoscope->outsize,
296 GST_PAD_CAPS (monoscope->srcpad), outbuf);
298 if (ret != GST_FLOW_OK)
302 return GST_FLOW_ERROR;
308 gst_monoscope_chain (GstPad * pad, GstBuffer * inbuf)
310 GstFlowReturn flow_ret = GST_FLOW_OK;
311 GstMonoscope *monoscope;
313 monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
315 /* don't try to combine samples from discont buffer */
316 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DISCONT)) {
317 gst_adapter_clear (monoscope->adapter);
318 monoscope->next_ts = GST_CLOCK_TIME_NONE;
321 /* Match timestamps from the incoming audio */
322 if (GST_BUFFER_TIMESTAMP (inbuf) != GST_CLOCK_TIME_NONE)
323 monoscope->next_ts = GST_BUFFER_TIMESTAMP (inbuf);
325 GST_LOG_OBJECT (monoscope, "in buffer has %d samples, ts=%" GST_TIME_FORMAT,
326 GST_BUFFER_SIZE (inbuf) / monoscope->bps,
327 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
329 gst_adapter_push (monoscope->adapter, inbuf);
332 /* Collect samples until we have enough for an output frame */
333 while (flow_ret == GST_FLOW_OK) {
335 GstBuffer *outbuf = NULL;
336 guint32 *pixels, avail, bytesperframe;
338 avail = gst_adapter_available (monoscope->adapter);
339 GST_LOG_OBJECT (monoscope, "bytes avail now %u", avail);
341 /* do negotiation if not done yet, so ->spf etc. is set */
342 if (GST_PAD_CAPS (monoscope->srcpad) == NULL) {
343 flow_ret = get_buffer (monoscope, &outbuf);
344 if (flow_ret != GST_FLOW_OK)
346 gst_buffer_unref (outbuf);
350 bytesperframe = monoscope->spf * monoscope->bps;
351 if (avail < bytesperframe)
354 /* FIXME: something is wrong with QoS, we are skipping way too much
355 * stuff even with very low CPU loads */
357 if (monoscope->next_ts != -1) {
361 qostime = gst_segment_to_running_time (&monoscope->segment,
362 GST_FORMAT_TIME, monoscope->next_ts);
364 GST_OBJECT_LOCK (monoscope);
365 /* check for QoS, don't compute buffers that are known to be late */
367 GST_CLOCK_TIME_IS_VALID (monoscope->earliest_time) &&
368 qostime <= monoscope->earliest_time;
369 GST_OBJECT_UNLOCK (monoscope);
372 GST_WARNING_OBJECT (monoscope,
373 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
374 GST_TIME_ARGS (qostime), GST_TIME_ARGS (monoscope->earliest_time));
380 samples = (gint16 *) gst_adapter_peek (monoscope->adapter, bytesperframe);
382 if (monoscope->spf < 512) {
383 gint16 in_data[512], i;
385 for (i = 0; i < 512; ++i) {
388 off = ((gdouble) i * (gdouble) monoscope->spf) / 512.0;
389 in_data[i] = samples[MIN ((guint) off, monoscope->spf)];
391 pixels = monoscope_update (monoscope->visstate, in_data);
393 /* not really correct, but looks much prettier */
394 pixels = monoscope_update (monoscope->visstate, samples);
397 flow_ret = get_buffer (monoscope, &outbuf);
398 if (flow_ret != GST_FLOW_OK)
401 memcpy (GST_BUFFER_DATA (outbuf), pixels, monoscope->outsize);
403 GST_BUFFER_TIMESTAMP (outbuf) = monoscope->next_ts;
404 GST_BUFFER_DURATION (outbuf) = monoscope->frame_duration;
406 flow_ret = gst_pad_push (monoscope->srcpad, outbuf);
412 if (GST_CLOCK_TIME_IS_VALID (monoscope->next_ts))
413 monoscope->next_ts += monoscope->frame_duration;
415 gst_adapter_flush (monoscope->adapter, bytesperframe);
424 gst_monoscope_sink_event (GstPad * pad, GstEvent * event)
426 GstMonoscope *monoscope;
429 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
431 switch (GST_EVENT_TYPE (event)) {
432 case GST_EVENT_FLUSH_START:
433 res = gst_pad_push_event (monoscope->srcpad, event);
435 case GST_EVENT_FLUSH_STOP:
436 gst_monoscope_reset (monoscope);
437 res = gst_pad_push_event (monoscope->srcpad, event);
439 case GST_EVENT_NEWSEGMENT:
443 gint64 start, stop, time;
446 /* the newsegment values are used to clip the input samples
447 * and to convert the incomming timestamps to running time so
449 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
450 &start, &stop, &time);
452 /* now configure the values */
453 gst_segment_set_newsegment_full (&monoscope->segment, update,
454 rate, arate, format, start, stop, time);
456 res = gst_pad_push_event (monoscope->srcpad, event);
460 res = gst_pad_push_event (monoscope->srcpad, event);
464 gst_object_unref (monoscope);
470 gst_monoscope_src_event (GstPad * pad, GstEvent * event)
472 GstMonoscope *monoscope;
475 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
477 switch (GST_EVENT_TYPE (event)) {
480 GstClockTimeDiff diff;
481 GstClockTime timestamp;
483 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
485 /* save stuff for the _chain() function */
486 GST_OBJECT_LOCK (monoscope);
487 monoscope->proportion = proportion;
489 /* we're late, this is a good estimate for next displayable
490 * frame (see part-qos.txt) */
491 monoscope->earliest_time =
492 timestamp + 2 * diff + monoscope->frame_duration;
494 monoscope->earliest_time = timestamp + diff;
495 GST_OBJECT_UNLOCK (monoscope);
497 res = gst_pad_push_event (monoscope->sinkpad, event);
501 res = gst_pad_push_event (monoscope->sinkpad, event);
504 gst_object_unref (monoscope);
509 static GstStateChangeReturn
510 gst_monoscope_change_state (GstElement * element, GstStateChange transition)
512 GstMonoscope *monoscope = GST_MONOSCOPE (element);
513 GstStateChangeReturn ret;
515 switch (transition) {
516 case GST_STATE_CHANGE_NULL_TO_READY:
518 case GST_STATE_CHANGE_READY_TO_PAUSED:
519 gst_monoscope_reset (monoscope);
525 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
527 switch (transition) {
528 case GST_STATE_CHANGE_PAUSED_TO_READY:
530 case GST_STATE_CHANGE_READY_TO_NULL:
540 plugin_init (GstPlugin * plugin)
542 GST_DEBUG_CATEGORY_INIT (monoscope_debug, "monoscope", 0,
543 "monoscope element");
545 return gst_element_register (plugin, "monoscope",
546 GST_RANK_NONE, GST_TYPE_MONOSCOPE);
549 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
552 "Monoscope visualization",
553 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);