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
24 * @see_also: monoscope
27 * <title>Example launch line</title>
30 * gst-launch -v audiotestsrc ! audioconvert ! monoscope ! ffmpegcolorspace ! ximagesink
40 #include <gst/video/video.h>
41 #include <gst/audio/audio.h>
43 #include "gstmonoscope.h"
44 #include "monoscope.h"
46 GST_DEBUG_CATEGORY_STATIC (monoscope_debug);
47 #define GST_CAT_DEFAULT monoscope_debug
49 /* elementfactory information */
50 static const GstElementDetails gst_monoscope_details =
51 GST_ELEMENT_DETAILS ("Monoscope",
53 "Displays a highly stabilised waveform of audio input",
54 "Richard Boulton <richard@tartarus.org>");
56 #if G_BYTE_ORDER == G_BIG_ENDIAN
57 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
60 GST_STATIC_CAPS ("video/x-raw-rgb, "
63 "endianness = (int) BIG_ENDIAN, "
64 "red_mask = (int) " GST_VIDEO_BYTE2_MASK_32 ", "
65 "green_mask = (int) " GST_VIDEO_BYTE3_MASK_32 ", "
66 "blue_mask = (int) " GST_VIDEO_BYTE4_MASK_32 ", "
68 "height = (int)128, " "framerate = " GST_VIDEO_FPS_RANGE)
71 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
74 GST_STATIC_CAPS ("video/x-raw-rgb, "
77 "endianness = (int) BIG_ENDIAN, "
78 "red_mask = (int) " GST_VIDEO_BYTE3_MASK_32 ", "
79 "green_mask = (int) " GST_VIDEO_BYTE2_MASK_32 ", "
80 "blue_mask = (int) " GST_VIDEO_BYTE1_MASK_32 ", "
82 "height = (int)128, " "framerate = " GST_VIDEO_FPS_RANGE)
86 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
89 GST_STATIC_CAPS (GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_CAPS)
93 GST_BOILERPLATE (GstMonoscope, gst_monoscope, GstElement, GST_TYPE_ELEMENT);
95 static void gst_monoscope_finalize (GObject * object);
96 static GstFlowReturn gst_monoscope_chain (GstPad * pad, GstBuffer * buf);
97 static gboolean gst_monoscope_src_setcaps (GstPad * pad, GstCaps * caps);
98 static gboolean gst_monoscope_sink_setcaps (GstPad * pad, GstCaps * caps);
99 static void gst_monoscope_reset (GstMonoscope * monoscope);
100 static gboolean gst_monoscope_sink_event (GstPad * pad, GstEvent * event);
101 static gboolean gst_monoscope_src_event (GstPad * pad, GstEvent * event);
102 static GstStateChangeReturn gst_monoscope_change_state (GstElement * element,
103 GstStateChange transition);
106 gst_monoscope_base_init (gpointer klass)
108 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&src_template));
112 gst_element_class_add_pad_template (element_class,
113 gst_static_pad_template_get (&sink_template));
114 gst_element_class_set_details (element_class, &gst_monoscope_details);
118 gst_monoscope_class_init (GstMonoscopeClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
126 gobject_class->finalize = gst_monoscope_finalize;
128 gstelement_class->change_state =
129 GST_DEBUG_FUNCPTR (gst_monoscope_change_state);
133 gst_monoscope_init (GstMonoscope * monoscope, GstMonoscopeClass * klass)
136 gst_pad_new_from_static_template (&sink_template, "sink");
137 gst_pad_set_chain_function (monoscope->sinkpad,
138 GST_DEBUG_FUNCPTR (gst_monoscope_chain));
139 gst_pad_set_event_function (monoscope->sinkpad,
140 GST_DEBUG_FUNCPTR (gst_monoscope_sink_event));
141 gst_pad_set_setcaps_function (monoscope->sinkpad,
142 GST_DEBUG_FUNCPTR (gst_monoscope_sink_setcaps));
143 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
145 monoscope->srcpad = gst_pad_new_from_static_template (&src_template, "src");
146 gst_pad_set_setcaps_function (monoscope->srcpad,
147 GST_DEBUG_FUNCPTR (gst_monoscope_src_setcaps));
148 gst_pad_set_event_function (monoscope->srcpad,
149 GST_DEBUG_FUNCPTR (gst_monoscope_src_event));
150 gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
152 monoscope->adapter = gst_adapter_new ();
153 monoscope->next_ts = GST_CLOCK_TIME_NONE;
154 monoscope->bps = sizeof (gint16);
156 /* reset the initial video state */
157 monoscope->width = 256;
158 monoscope->height = 128;
159 monoscope->fps_num = 25; /* desired frame rate */
160 monoscope->fps_denom = 1;
161 monoscope->visstate = NULL;
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, *intersect;
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 intersect = gst_caps_intersect (othercaps, templ);
254 gst_caps_unref (othercaps);
256 if (gst_caps_is_empty (intersect))
259 target = gst_caps_copy_nth (intersect, 0);
260 gst_caps_unref (intersect);
262 target = gst_caps_ref ((GstCaps *) templ);
265 structure = gst_caps_get_structure (target, 0);
266 gst_structure_fixate_field_nearest_int (structure, "width", 320);
267 gst_structure_fixate_field_nearest_int (structure, "height", 240);
268 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
270 gst_pad_set_caps (monoscope->srcpad, target);
271 gst_caps_unref (target);
277 gst_caps_unref (intersect);
283 get_buffer (GstMonoscope * monoscope, GstBuffer ** outbuf)
287 if (GST_PAD_CAPS (monoscope->srcpad) == NULL) {
288 if (!gst_monoscope_src_negotiate (monoscope))
289 return GST_FLOW_NOT_NEGOTIATED;
292 GST_LOG_OBJECT (monoscope, "allocating output buffer of size %d with caps %"
293 GST_PTR_FORMAT, monoscope->outsize, GST_PAD_CAPS (monoscope->srcpad));
296 gst_pad_alloc_buffer_and_set_caps (monoscope->srcpad,
297 GST_BUFFER_OFFSET_NONE, monoscope->outsize,
298 GST_PAD_CAPS (monoscope->srcpad), outbuf);
300 if (ret != GST_FLOW_OK)
304 return GST_FLOW_ERROR;
310 gst_monoscope_chain (GstPad * pad, GstBuffer * inbuf)
312 GstMonoscope *monoscope;
313 GstFlowReturn flow_ret;
315 monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
317 /* don't try to combine samples from discont buffer */
318 if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DISCONT)) {
319 gst_adapter_clear (monoscope->adapter);
320 monoscope->next_ts = GST_CLOCK_TIME_NONE;
323 /* Match timestamps from the incoming audio */
324 if (GST_BUFFER_TIMESTAMP (inbuf) != GST_CLOCK_TIME_NONE)
325 monoscope->next_ts = GST_BUFFER_TIMESTAMP (inbuf);
327 GST_LOG_OBJECT (monoscope, "in buffer has %d samples, ts=%" GST_TIME_FORMAT,
328 GST_BUFFER_SIZE (inbuf) / monoscope->bps,
329 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
331 gst_adapter_push (monoscope->adapter, inbuf);
334 flow_ret = GST_FLOW_OK;
336 /* Collect samples until we have enough for an output frame */
337 while (flow_ret == GST_FLOW_OK) {
339 GstBuffer *outbuf = NULL;
340 guint32 *pixels, avail, bytesperframe;
342 avail = gst_adapter_available (monoscope->adapter);
343 GST_LOG_OBJECT (monoscope, "bytes avail now %u", avail);
345 /* do negotiation if not done yet, so ->spf etc. is set */
346 if (GST_PAD_CAPS (monoscope->srcpad) == NULL) {
347 flow_ret = get_buffer (monoscope, &outbuf);
348 if (flow_ret != GST_FLOW_OK) {
349 gst_buffer_unref (inbuf);
352 gst_buffer_unref (outbuf);
356 bytesperframe = monoscope->spf * monoscope->bps;
357 if (avail < bytesperframe)
360 /* FIXME: something is wrong with QoS, we are skipping way too much
361 * stuff even with very low CPU loads */
363 if (monoscope->next_ts != -1) {
367 qostime = gst_segment_to_running_time (&monoscope->segment,
368 GST_FORMAT_TIME, monoscope->next_ts);
370 GST_OBJECT_LOCK (monoscope);
371 /* check for QoS, don't compute buffers that are known to be late */
373 GST_CLOCK_TIME_IS_VALID (monoscope->earliest_time) &&
374 qostime <= monoscope->earliest_time;
375 GST_OBJECT_UNLOCK (monoscope);
378 GST_WARNING_OBJECT (monoscope,
379 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
380 GST_TIME_ARGS (qostime), GST_TIME_ARGS (monoscope->earliest_time));
386 samples = (gint16 *) gst_adapter_peek (monoscope->adapter, bytesperframe);
388 if (monoscope->spf < 512) {
389 gint16 in_data[512], i;
391 for (i = 0; i < 512; ++i) {
394 off = ((gdouble) i * (gdouble) monoscope->spf) / 512.0;
395 in_data[i] = samples[MIN ((guint) off, monoscope->spf)];
397 pixels = monoscope_update (monoscope->visstate, in_data);
399 /* not really correct, but looks much prettier */
400 pixels = monoscope_update (monoscope->visstate, samples);
403 flow_ret = get_buffer (monoscope, &outbuf);
404 if (flow_ret != GST_FLOW_OK) {
405 gst_buffer_unref (inbuf);
409 memcpy (GST_BUFFER_DATA (outbuf), pixels, monoscope->outsize);
411 GST_BUFFER_TIMESTAMP (outbuf) = monoscope->next_ts;
412 GST_BUFFER_DURATION (outbuf) = monoscope->frame_duration;
414 flow_ret = gst_pad_push (monoscope->srcpad, outbuf);
420 if (GST_CLOCK_TIME_IS_VALID (monoscope->next_ts))
421 monoscope->next_ts += monoscope->frame_duration;
423 gst_adapter_flush (monoscope->adapter, bytesperframe);
432 gst_monoscope_sink_event (GstPad * pad, GstEvent * event)
434 GstMonoscope *monoscope;
437 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
439 switch (GST_EVENT_TYPE (event)) {
440 case GST_EVENT_FLUSH_START:
441 res = gst_pad_push_event (monoscope->srcpad, event);
443 case GST_EVENT_FLUSH_STOP:
444 gst_monoscope_reset (monoscope);
445 res = gst_pad_push_event (monoscope->srcpad, event);
447 case GST_EVENT_NEWSEGMENT:
451 gint64 start, stop, time;
454 /* the newsegment values are used to clip the input samples
455 * and to convert the incomming timestamps to running time so
457 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
458 &start, &stop, &time);
460 /* now configure the values */
461 gst_segment_set_newsegment_full (&monoscope->segment, update,
462 rate, arate, format, start, stop, time);
464 res = gst_pad_push_event (monoscope->srcpad, event);
468 res = gst_pad_push_event (monoscope->srcpad, event);
472 gst_object_unref (monoscope);
478 gst_monoscope_src_event (GstPad * pad, GstEvent * event)
480 GstMonoscope *monoscope;
483 monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
485 switch (GST_EVENT_TYPE (event)) {
488 GstClockTimeDiff diff;
489 GstClockTime timestamp;
491 gst_event_parse_qos (event, &proportion, &diff, ×tamp);
493 /* save stuff for the _chain() function */
494 GST_OBJECT_LOCK (monoscope);
495 monoscope->proportion = proportion;
497 /* we're late, this is a good estimate for next displayable
498 * frame (see part-qos.txt) */
499 monoscope->earliest_time =
500 timestamp + 2 * diff + monoscope->frame_duration;
502 monoscope->earliest_time = timestamp + diff;
503 GST_OBJECT_UNLOCK (monoscope);
505 res = gst_pad_push_event (monoscope->sinkpad, event);
509 res = gst_pad_push_event (monoscope->sinkpad, event);
512 gst_object_unref (monoscope);
517 static GstStateChangeReturn
518 gst_monoscope_change_state (GstElement * element, GstStateChange transition)
520 GstMonoscope *monoscope = GST_MONOSCOPE (element);
521 GstStateChangeReturn ret;
524 switch (transition) {
525 case GST_STATE_CHANGE_NULL_TO_READY:
527 case GST_STATE_CHANGE_READY_TO_PAUSED:
528 gst_monoscope_reset (monoscope);
534 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
536 switch (transition) {
537 case GST_STATE_CHANGE_PAUSED_TO_READY:
539 case GST_STATE_CHANGE_READY_TO_NULL:
549 plugin_init (GstPlugin * plugin)
551 GST_DEBUG_CATEGORY_INIT (monoscope_debug, "monoscope", 0,
552 "monoscope element");
554 return gst_element_register (plugin, "monoscope",
555 GST_RANK_NONE, GST_TYPE_MONOSCOPE);
558 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
561 "Monoscope visualization",
562 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);