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_pad_template (element_class,
105 gst_static_pad_template_get (&src_template));
106 gst_element_class_add_pad_template (element_class,
107 gst_static_pad_template_get (&sink_template));
108 gst_element_class_set_details_simple (element_class, "Monoscope",
110 "Displays a highly stabilised waveform of audio input",
111 "Richard Boulton <richard@tartarus.org>");
115 gst_monoscope_class_init (GstMonoscopeClass * klass)
117 GObjectClass *gobject_class;
118 GstElementClass *gstelement_class;
120 gobject_class = (GObjectClass *) klass;
121 gstelement_class = (GstElementClass *) klass;
123 gobject_class->finalize = gst_monoscope_finalize;
125 gstelement_class->change_state =
126 GST_DEBUG_FUNCPTR (gst_monoscope_change_state);
130 gst_monoscope_init (GstMonoscope * monoscope, GstMonoscopeClass * klass)
133 gst_pad_new_from_static_template (&sink_template, "sink");
134 gst_pad_set_chain_function (monoscope->sinkpad,
135 GST_DEBUG_FUNCPTR (gst_monoscope_chain));
136 gst_pad_set_event_function (monoscope->sinkpad,
137 GST_DEBUG_FUNCPTR (gst_monoscope_sink_event));
138 gst_pad_set_setcaps_function (monoscope->sinkpad,
139 GST_DEBUG_FUNCPTR (gst_monoscope_sink_setcaps));
140 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
142 monoscope->srcpad = gst_pad_new_from_static_template (&src_template, "src");
143 gst_pad_set_setcaps_function (monoscope->srcpad,
144 GST_DEBUG_FUNCPTR (gst_monoscope_src_setcaps));
145 gst_pad_set_event_function (monoscope->srcpad,
146 GST_DEBUG_FUNCPTR (gst_monoscope_src_event));
147 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
149 monoscope->adapter = gst_adapter_new ();
150 monoscope->next_ts = GST_CLOCK_TIME_NONE;
151 monoscope->bps = sizeof (gint16);
153 /* reset the initial video state */
154 monoscope->width = 256;
155 monoscope->height = 128;
156 monoscope->fps_num = 25; /* desired frame rate */
157 monoscope->fps_denom = 1;
158 monoscope->visstate = NULL;
160 /* reset the initial audio state */
161 monoscope->rate = GST_AUDIO_DEF_RATE;
165 gst_monoscope_finalize (GObject * object)
167 GstMonoscope *monoscope = GST_MONOSCOPE (object);
169 if (monoscope->visstate)
170 monoscope_close (monoscope->visstate);
172 g_object_unref (monoscope->adapter);
174 G_OBJECT_CLASS (parent_class)->finalize (object);
178 gst_monoscope_reset (GstMonoscope * monoscope)
180 monoscope->next_ts = GST_CLOCK_TIME_NONE;
182 gst_adapter_clear (monoscope->adapter);
183 gst_segment_init (&monoscope->segment, GST_FORMAT_UNDEFINED);
185 GST_OBJECT_LOCK (monoscope);
186 monoscope->proportion = 1.0;
187 monoscope->earliest_time = -1;
188 GST_OBJECT_UNLOCK (monoscope);
192 gst_monoscope_sink_setcaps (GstPad * pad, GstCaps * caps)
194 GstMonoscope *monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
195 GstStructure *structure;
197 structure = gst_caps_get_structure (caps, 0);
199 gst_structure_get_int (structure, "rate", &monoscope->rate);
201 GST_DEBUG_OBJECT (monoscope, "sample rate = %d", monoscope->rate);
206 gst_monoscope_src_setcaps (GstPad * pad, GstCaps * caps)
208 GstMonoscope *monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
209 GstStructure *structure;
211 structure = gst_caps_get_structure (caps, 0);
213 gst_structure_get_int (structure, "width", &monoscope->width);
214 gst_structure_get_int (structure, "height", &monoscope->height);
215 gst_structure_get_fraction (structure, "framerate", &monoscope->fps_num,
216 &monoscope->fps_denom);
218 monoscope->outsize = monoscope->width * monoscope->height * 4;
219 monoscope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
220 monoscope->fps_denom, monoscope->fps_num);
222 gst_util_uint64_scale_int (monoscope->rate, monoscope->fps_denom,
225 GST_DEBUG_OBJECT (monoscope, "dimension %dx%d, framerate %d/%d, spf %d",
226 monoscope->width, monoscope->height, monoscope->fps_num,
227 monoscope->fps_denom, monoscope->spf);
229 if (monoscope->visstate) {
230 monoscope_close (monoscope->visstate);
231 monoscope->visstate = NULL;
234 monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
236 return (monoscope->visstate != NULL);
240 gst_monoscope_src_negotiate (GstMonoscope * monoscope)
242 GstCaps *othercaps, *target;
243 GstStructure *structure;
244 const GstCaps *templ;
246 templ = gst_pad_get_pad_template_caps (monoscope->srcpad);
248 GST_DEBUG_OBJECT (monoscope, "performing negotiation");
250 /* see what the peer can do */
251 othercaps = gst_pad_peer_get_caps (monoscope->srcpad);
253 target = gst_caps_intersect (othercaps, templ);
254 gst_caps_unref (othercaps);
256 if (gst_caps_is_empty (target))
259 gst_caps_truncate (target);
261 target = gst_caps_ref ((GstCaps *) templ);
264 structure = gst_caps_get_structure (target, 0);
265 gst_structure_fixate_field_nearest_int (structure, "width", 320);
266 gst_structure_fixate_field_nearest_int (structure, "height", 240);
267 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
269 gst_pad_set_caps (monoscope->srcpad, target);
270 gst_caps_unref (target);
276 gst_caps_unref (target);
282 get_buffer (GstMonoscope * monoscope, GstBuffer ** outbuf)
286 if (GST_PAD_CAPS (monoscope->srcpad) == NULL) {
287 if (!gst_monoscope_src_negotiate (monoscope))
288 return GST_FLOW_NOT_NEGOTIATED;
291 GST_LOG_OBJECT (monoscope, "allocating output buffer of size %d with caps %"
292 GST_PTR_FORMAT, monoscope->outsize, GST_PAD_CAPS (monoscope->srcpad));
295 gst_pad_alloc_buffer_and_set_caps (monoscope->srcpad,
296 GST_BUFFER_OFFSET_NONE, monoscope->outsize,
297 GST_PAD_CAPS (monoscope->srcpad), outbuf);
299 if (ret != GST_FLOW_OK)
303 return GST_FLOW_ERROR;
309 gst_monoscope_chain (GstPad * pad, GstBuffer * inbuf)
311 GstFlowReturn flow_ret = GST_FLOW_OK;
312 GstMonoscope *monoscope;
314 monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
316 /* don't try to combine samples from discont buffer */
317 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DISCONT)) {
318 gst_adapter_clear (monoscope->adapter);
319 monoscope->next_ts = GST_CLOCK_TIME_NONE;
322 /* Match timestamps from the incoming audio */
323 if (GST_BUFFER_TIMESTAMP (inbuf) != GST_CLOCK_TIME_NONE)
324 monoscope->next_ts = GST_BUFFER_TIMESTAMP (inbuf);
326 GST_LOG_OBJECT (monoscope, "in buffer has %d samples, ts=%" GST_TIME_FORMAT,
327 GST_BUFFER_SIZE (inbuf) / monoscope->bps,
328 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
330 gst_adapter_push (monoscope->adapter, inbuf);
333 /* Collect samples until we have enough for an output frame */
334 while (flow_ret == GST_FLOW_OK) {
336 GstBuffer *outbuf = NULL;
337 guint32 *pixels, avail, bytesperframe;
339 avail = gst_adapter_available (monoscope->adapter);
340 GST_LOG_OBJECT (monoscope, "bytes avail now %u", avail);
342 /* do negotiation if not done yet, so ->spf etc. is set */
343 if (GST_PAD_CAPS (monoscope->srcpad) == NULL) {
344 flow_ret = get_buffer (monoscope, &outbuf);
345 if (flow_ret != GST_FLOW_OK)
347 gst_buffer_unref (outbuf);
351 bytesperframe = monoscope->spf * monoscope->bps;
352 if (avail < bytesperframe)
355 /* FIXME: something is wrong with QoS, we are skipping way too much
356 * stuff even with very low CPU loads */
358 if (monoscope->next_ts != -1) {
362 qostime = gst_segment_to_running_time (&monoscope->segment,
363 GST_FORMAT_TIME, monoscope->next_ts);
365 GST_OBJECT_LOCK (monoscope);
366 /* check for QoS, don't compute buffers that are known to be late */
368 GST_CLOCK_TIME_IS_VALID (monoscope->earliest_time) &&
369 qostime <= monoscope->earliest_time;
370 GST_OBJECT_UNLOCK (monoscope);
373 GST_WARNING_OBJECT (monoscope,
374 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
375 GST_TIME_ARGS (qostime), GST_TIME_ARGS (monoscope->earliest_time));
381 samples = (gint16 *) gst_adapter_peek (monoscope->adapter, bytesperframe);
383 if (monoscope->spf < 512) {
384 gint16 in_data[512], i;
386 for (i = 0; i < 512; ++i) {
389 off = ((gdouble) i * (gdouble) monoscope->spf) / 512.0;
390 in_data[i] = samples[MIN ((guint) off, monoscope->spf)];
392 pixels = monoscope_update (monoscope->visstate, in_data);
394 /* not really correct, but looks much prettier */
395 pixels = monoscope_update (monoscope->visstate, samples);
398 flow_ret = get_buffer (monoscope, &outbuf);
399 if (flow_ret != GST_FLOW_OK)
402 memcpy (GST_BUFFER_DATA (outbuf), pixels, monoscope->outsize);
404 GST_BUFFER_TIMESTAMP (outbuf) = monoscope->next_ts;
405 GST_BUFFER_DURATION (outbuf) = monoscope->frame_duration;
407 flow_ret = gst_pad_push (monoscope->srcpad, outbuf);
413 if (GST_CLOCK_TIME_IS_VALID (monoscope->next_ts))
414 monoscope->next_ts += monoscope->frame_duration;
416 gst_adapter_flush (monoscope->adapter, bytesperframe);
425 gst_monoscope_sink_event (GstPad * pad, GstEvent * event)
427 GstMonoscope *monoscope;
430 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
432 switch (GST_EVENT_TYPE (event)) {
433 case GST_EVENT_FLUSH_START:
434 res = gst_pad_push_event (monoscope->srcpad, event);
436 case GST_EVENT_FLUSH_STOP:
437 gst_monoscope_reset (monoscope);
438 res = gst_pad_push_event (monoscope->srcpad, event);
440 case GST_EVENT_NEWSEGMENT:
444 gint64 start, stop, time;
447 /* the newsegment values are used to clip the input samples
448 * and to convert the incomming timestamps to running time so
450 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
451 &start, &stop, &time);
453 /* now configure the values */
454 gst_segment_set_newsegment_full (&monoscope->segment, update,
455 rate, arate, format, start, stop, time);
457 res = gst_pad_push_event (monoscope->srcpad, event);
461 res = gst_pad_push_event (monoscope->srcpad, event);
465 gst_object_unref (monoscope);
471 gst_monoscope_src_event (GstPad * pad, GstEvent * event)
473 GstMonoscope *monoscope;
476 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
478 switch (GST_EVENT_TYPE (event)) {
481 GstClockTimeDiff diff;
482 GstClockTime timestamp;
484 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
486 /* save stuff for the _chain() function */
487 GST_OBJECT_LOCK (monoscope);
488 monoscope->proportion = proportion;
490 /* we're late, this is a good estimate for next displayable
491 * frame (see part-qos.txt) */
492 monoscope->earliest_time =
493 timestamp + 2 * diff + monoscope->frame_duration;
495 monoscope->earliest_time = timestamp + diff;
496 GST_OBJECT_UNLOCK (monoscope);
498 res = gst_pad_push_event (monoscope->sinkpad, event);
502 res = gst_pad_push_event (monoscope->sinkpad, event);
505 gst_object_unref (monoscope);
510 static GstStateChangeReturn
511 gst_monoscope_change_state (GstElement * element, GstStateChange transition)
513 GstMonoscope *monoscope = GST_MONOSCOPE (element);
514 GstStateChangeReturn ret;
516 switch (transition) {
517 case GST_STATE_CHANGE_NULL_TO_READY:
519 case GST_STATE_CHANGE_READY_TO_PAUSED:
520 gst_monoscope_reset (monoscope);
526 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
528 switch (transition) {
529 case GST_STATE_CHANGE_PAUSED_TO_READY:
531 case GST_STATE_CHANGE_READY_TO_NULL:
541 plugin_init (GstPlugin * plugin)
543 GST_DEBUG_CATEGORY_INIT (monoscope_debug, "monoscope", 0,
544 "monoscope element");
546 return gst_element_register (plugin, "monoscope",
547 GST_RANK_NONE, GST_TYPE_MONOSCOPE);
550 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
553 "Monoscope visualization",
554 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);