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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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-1.0 -v audiotestsrc ! audioconvert ! monoscope ! videoconvert ! 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 #define RGB_ORDER "xRGB"
53 #define RGB_ORDER "BGRx"
56 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
59 GST_STATIC_CAPS ("video/x-raw, "
60 "format = (string) " RGB_ORDER ", "
61 "width = " G_STRINGIFY (scope_width) ", "
62 "height = " G_STRINGIFY (scope_height) ", "
63 "framerate = " GST_VIDEO_FPS_RANGE)
66 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
69 GST_STATIC_CAPS ("audio/x-raw, "
70 "format = (string) " GST_AUDIO_NE (S16) ", "
71 "rate = (int) [ 8000, 96000 ], "
72 "channels = (int) 1, " "layout = (string) interleaved")
76 #define gst_monoscope_parent_class parent_class
77 G_DEFINE_TYPE (GstMonoscope, gst_monoscope, GST_TYPE_ELEMENT);
79 static void gst_monoscope_finalize (GObject * object);
80 static GstFlowReturn gst_monoscope_chain (GstPad * pad, GstObject * parent,
82 static gboolean gst_monoscope_src_setcaps (GstMonoscope * mono, GstCaps * caps);
83 static gboolean gst_monoscope_sink_setcaps (GstMonoscope * mono,
85 static void gst_monoscope_reset (GstMonoscope * monoscope);
86 static gboolean gst_monoscope_sink_event (GstPad * pad, GstObject * parent,
88 static gboolean gst_monoscope_src_event (GstPad * pad, GstObject * parent,
90 static GstStateChangeReturn gst_monoscope_change_state (GstElement * element,
91 GstStateChange transition);
94 gst_monoscope_class_init (GstMonoscopeClass * klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
99 gobject_class = (GObjectClass *) klass;
100 gstelement_class = (GstElementClass *) klass;
102 gobject_class->finalize = gst_monoscope_finalize;
104 gstelement_class->change_state =
105 GST_DEBUG_FUNCPTR (gst_monoscope_change_state);
107 gst_element_class_add_static_pad_template (gstelement_class, &src_template);
108 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
109 gst_element_class_set_static_metadata (gstelement_class, "Monoscope",
111 "Displays a highly stabilised waveform of audio input",
112 "Richard Boulton <richard@tartarus.org>");
116 gst_monoscope_init (GstMonoscope * monoscope)
119 gst_pad_new_from_static_template (&sink_template, "sink");
120 gst_pad_set_chain_function (monoscope->sinkpad,
121 GST_DEBUG_FUNCPTR (gst_monoscope_chain));
122 gst_pad_set_event_function (monoscope->sinkpad,
123 GST_DEBUG_FUNCPTR (gst_monoscope_sink_event));
124 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
126 monoscope->srcpad = gst_pad_new_from_static_template (&src_template, "src");
127 gst_pad_set_event_function (monoscope->srcpad,
128 GST_DEBUG_FUNCPTR (gst_monoscope_src_event));
129 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
131 monoscope->adapter = gst_adapter_new ();
132 monoscope->next_ts = GST_CLOCK_TIME_NONE;
133 monoscope->bps = sizeof (gint16);
135 /* reset the initial video state */
136 monoscope->width = scope_width;
137 monoscope->height = scope_height;
138 monoscope->fps_num = 25; /* desired frame rate */
139 monoscope->fps_denom = 1;
140 monoscope->visstate = NULL;
142 /* reset the initial audio state */
143 monoscope->rate = GST_AUDIO_DEF_RATE;
147 gst_monoscope_finalize (GObject * object)
149 GstMonoscope *monoscope = GST_MONOSCOPE (object);
151 if (monoscope->visstate)
152 monoscope_close (monoscope->visstate);
154 g_object_unref (monoscope->adapter);
156 G_OBJECT_CLASS (parent_class)->finalize (object);
160 gst_monoscope_reset (GstMonoscope * monoscope)
162 monoscope->next_ts = GST_CLOCK_TIME_NONE;
164 gst_adapter_clear (monoscope->adapter);
165 gst_segment_init (&monoscope->segment, GST_FORMAT_UNDEFINED);
166 monoscope->segment_pending = FALSE;
168 GST_OBJECT_LOCK (monoscope);
169 monoscope->proportion = 1.0;
170 monoscope->earliest_time = -1;
171 GST_OBJECT_UNLOCK (monoscope);
175 gst_monoscope_sink_setcaps (GstMonoscope * monoscope, GstCaps * caps)
177 GstStructure *structure;
179 structure = gst_caps_get_structure (caps, 0);
181 gst_structure_get_int (structure, "rate", &monoscope->rate);
183 GST_DEBUG_OBJECT (monoscope, "sample rate = %d", monoscope->rate);
188 gst_monoscope_src_setcaps (GstMonoscope * monoscope, GstCaps * caps)
190 GstStructure *structure;
193 structure = gst_caps_get_structure (caps, 0);
195 gst_structure_get_int (structure, "width", &monoscope->width);
196 gst_structure_get_int (structure, "height", &monoscope->height);
197 gst_structure_get_fraction (structure, "framerate", &monoscope->fps_num,
198 &monoscope->fps_denom);
200 monoscope->outsize = monoscope->width * monoscope->height * 4;
201 monoscope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
202 monoscope->fps_denom, monoscope->fps_num);
204 gst_util_uint64_scale_int (monoscope->rate, monoscope->fps_denom,
207 GST_DEBUG_OBJECT (monoscope, "dimension %dx%d, framerate %d/%d, spf %d",
208 monoscope->width, monoscope->height, monoscope->fps_num,
209 monoscope->fps_denom, monoscope->spf);
211 if (monoscope->visstate) {
212 monoscope_close (monoscope->visstate);
213 monoscope->visstate = NULL;
216 monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
218 res = gst_pad_set_caps (monoscope->srcpad, caps);
220 return res && (monoscope->visstate != NULL);
224 gst_monoscope_src_negotiate (GstMonoscope * monoscope)
226 GstCaps *othercaps, *target;
227 GstStructure *structure;
231 GstStructure *config;
232 guint size, min, max;
234 templ = gst_pad_get_pad_template_caps (monoscope->srcpad);
236 GST_DEBUG_OBJECT (monoscope, "performing negotiation");
238 /* see what the peer can do */
239 othercaps = gst_pad_peer_query_caps (monoscope->srcpad, NULL);
241 target = gst_caps_intersect (othercaps, templ);
242 gst_caps_unref (othercaps);
243 gst_caps_unref (templ);
245 if (gst_caps_is_empty (target))
248 target = gst_caps_truncate (target);
253 target = gst_caps_make_writable (target);
254 structure = gst_caps_get_structure (target, 0);
255 gst_structure_fixate_field_nearest_int (structure, "width", 320);
256 gst_structure_fixate_field_nearest_int (structure, "height", 240);
257 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
259 gst_monoscope_src_setcaps (monoscope, target);
261 /* try to get a bufferpool now */
262 /* find a pool for the negotiated caps now */
263 query = gst_query_new_allocation (target, TRUE);
265 if (!gst_pad_peer_query (monoscope->srcpad, query)) {
268 if (gst_query_get_n_allocation_pools (query) > 0) {
269 /* we got configuration from our peer, parse them */
270 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
273 size = monoscope->outsize;
278 /* we did not get a pool, make one ourselves then */
279 pool = gst_buffer_pool_new ();
282 config = gst_buffer_pool_get_config (pool);
283 gst_buffer_pool_config_set_params (config, target, size, min, max);
284 gst_buffer_pool_set_config (pool, config);
286 if (monoscope->pool) {
287 gst_buffer_pool_set_active (monoscope->pool, TRUE);
288 gst_object_unref (monoscope->pool);
290 monoscope->pool = pool;
293 gst_buffer_pool_set_active (pool, TRUE);
295 gst_query_unref (query);
296 gst_caps_unref (target);
302 gst_caps_unref (target);
307 /* make sure we are negotiated */
309 ensure_negotiated (GstMonoscope * monoscope)
311 gboolean reconfigure;
313 reconfigure = gst_pad_check_reconfigure (monoscope->srcpad);
315 /* we don't know an output format yet, pick one */
316 if (reconfigure || !gst_pad_has_current_caps (monoscope->srcpad)) {
317 if (!gst_monoscope_src_negotiate (monoscope)) {
318 gst_pad_mark_reconfigure (monoscope->srcpad);
319 if (GST_PAD_IS_FLUSHING (monoscope->srcpad))
320 return GST_FLOW_FLUSHING;
322 return GST_FLOW_NOT_NEGOTIATED;
329 gst_monoscope_chain (GstPad * pad, GstObject * parent, GstBuffer * inbuf)
331 GstFlowReturn flow_ret = GST_FLOW_OK;
332 GstMonoscope *monoscope;
334 monoscope = GST_MONOSCOPE (parent);
336 if (monoscope->rate == 0) {
337 gst_buffer_unref (inbuf);
338 flow_ret = GST_FLOW_NOT_NEGOTIATED;
342 /* Make sure have an output format */
343 flow_ret = ensure_negotiated (monoscope);
344 if (flow_ret != GST_FLOW_OK) {
345 gst_buffer_unref (inbuf);
349 if (monoscope->segment_pending) {
350 gst_pad_push_event (monoscope->srcpad,
351 gst_event_new_segment (&monoscope->segment));
352 monoscope->segment_pending = FALSE;
355 /* don't try to combine samples from discont buffer */
356 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DISCONT)) {
357 gst_adapter_clear (monoscope->adapter);
358 monoscope->next_ts = GST_CLOCK_TIME_NONE;
361 /* Match timestamps from the incoming audio */
362 if (GST_BUFFER_TIMESTAMP (inbuf) != GST_CLOCK_TIME_NONE)
363 monoscope->next_ts = GST_BUFFER_TIMESTAMP (inbuf);
365 GST_LOG_OBJECT (monoscope,
366 "in buffer has %" G_GSIZE_FORMAT " samples, ts=%" GST_TIME_FORMAT,
367 gst_buffer_get_size (inbuf) / monoscope->bps,
368 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
370 gst_adapter_push (monoscope->adapter, inbuf);
373 /* Collect samples until we have enough for an output frame */
374 while (flow_ret == GST_FLOW_OK) {
376 GstBuffer *outbuf = NULL;
377 guint32 *pixels, avail, bytesperframe;
379 avail = gst_adapter_available (monoscope->adapter);
380 GST_LOG_OBJECT (monoscope, "bytes avail now %u", avail);
382 bytesperframe = monoscope->spf * monoscope->bps;
383 if (avail < bytesperframe)
386 /* FIXME: something is wrong with QoS, we are skipping way too much
387 * stuff even with very low CPU loads */
389 if (monoscope->next_ts != -1) {
393 qostime = gst_segment_to_running_time (&monoscope->segment,
394 GST_FORMAT_TIME, monoscope->next_ts);
396 GST_OBJECT_LOCK (monoscope);
397 /* check for QoS, don't compute buffers that are known to be late */
399 GST_CLOCK_TIME_IS_VALID (monoscope->earliest_time) &&
400 qostime <= monoscope->earliest_time;
401 GST_OBJECT_UNLOCK (monoscope);
404 GST_WARNING_OBJECT (monoscope,
405 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
406 GST_TIME_ARGS (qostime), GST_TIME_ARGS (monoscope->earliest_time));
412 samples = (gint16 *) gst_adapter_map (monoscope->adapter, bytesperframe);
414 if (monoscope->spf < convolver_big) {
415 gint16 in_data[convolver_big], i;
416 gdouble scale = (gdouble) monoscope->spf / (gdouble) convolver_big;
418 for (i = 0; i < convolver_big; ++i) {
419 gdouble off = (gdouble) i * scale;
420 in_data[i] = samples[MIN ((guint) off, monoscope->spf)];
422 pixels = monoscope_update (monoscope->visstate, in_data);
424 /* not really correct, but looks much prettier */
425 pixels = monoscope_update (monoscope->visstate, samples);
428 GST_LOG_OBJECT (monoscope, "allocating output buffer");
429 flow_ret = gst_buffer_pool_acquire_buffer (monoscope->pool, &outbuf, NULL);
430 if (flow_ret != GST_FLOW_OK) {
431 gst_adapter_unmap (monoscope->adapter);
435 gst_buffer_fill (outbuf, 0, pixels, monoscope->outsize);
437 GST_BUFFER_TIMESTAMP (outbuf) = monoscope->next_ts;
438 GST_BUFFER_DURATION (outbuf) = monoscope->frame_duration;
440 flow_ret = gst_pad_push (monoscope->srcpad, outbuf);
446 if (GST_CLOCK_TIME_IS_VALID (monoscope->next_ts))
447 monoscope->next_ts += monoscope->frame_duration;
449 gst_adapter_flush (monoscope->adapter, bytesperframe);
458 gst_monoscope_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
460 GstMonoscope *monoscope;
463 monoscope = GST_MONOSCOPE (parent);
465 switch (GST_EVENT_TYPE (event)) {
466 case GST_EVENT_FLUSH_START:
467 res = gst_pad_push_event (monoscope->srcpad, event);
469 case GST_EVENT_FLUSH_STOP:
470 gst_monoscope_reset (monoscope);
471 res = gst_pad_push_event (monoscope->srcpad, event);
473 case GST_EVENT_SEGMENT:
475 /* the newsegment values are used to clip the input samples
476 * and to convert the incomming timestamps to running time so
478 gst_event_copy_segment (event, &monoscope->segment);
480 /* We forward the event from the chain function after caps are
481 * negotiated. Otherwise we would potentially break the event order and
482 * send the segment event before the caps event */
483 monoscope->segment_pending = TRUE;
484 gst_event_unref (event);
492 gst_event_parse_caps (event, &caps);
493 gst_monoscope_sink_setcaps (monoscope, caps);
494 gst_event_unref (event);
499 res = gst_pad_push_event (monoscope->srcpad, event);
507 gst_monoscope_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
509 GstMonoscope *monoscope;
512 monoscope = GST_MONOSCOPE (parent);
514 switch (GST_EVENT_TYPE (event)) {
517 GstClockTimeDiff diff;
518 GstClockTime timestamp;
520 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
522 /* save stuff for the _chain() function */
523 GST_OBJECT_LOCK (monoscope);
524 monoscope->proportion = proportion;
526 /* we're late, this is a good estimate for next displayable
527 * frame (see part-qos.txt) */
528 monoscope->earliest_time =
529 timestamp + 2 * diff + monoscope->frame_duration;
531 monoscope->earliest_time = timestamp + diff;
532 GST_OBJECT_UNLOCK (monoscope);
534 res = gst_pad_push_event (monoscope->sinkpad, event);
538 res = gst_pad_push_event (monoscope->sinkpad, event);
545 static GstStateChangeReturn
546 gst_monoscope_change_state (GstElement * element, GstStateChange transition)
548 GstMonoscope *monoscope = GST_MONOSCOPE (element);
549 GstStateChangeReturn ret;
551 switch (transition) {
552 case GST_STATE_CHANGE_NULL_TO_READY:
554 case GST_STATE_CHANGE_READY_TO_PAUSED:
555 gst_monoscope_reset (monoscope);
561 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
563 switch (transition) {
564 case GST_STATE_CHANGE_PAUSED_TO_READY:
565 if (monoscope->pool) {
566 gst_buffer_pool_set_active (monoscope->pool, FALSE);
567 gst_object_replace ((GstObject **) & monoscope->pool, NULL);
570 case GST_STATE_CHANGE_READY_TO_NULL:
580 plugin_init (GstPlugin * plugin)
582 GST_DEBUG_CATEGORY_INIT (monoscope_debug, "monoscope", 0,
583 "monoscope element");
585 return gst_element_register (plugin, "monoscope",
586 GST_RANK_NONE, GST_TYPE_MONOSCOPE);
589 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
592 "Monoscope visualization",
593 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);