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>
5  *
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.
10  *
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.
15  *
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.
20  */
21 
22 /**
23  * SECTION:element-monoscope
24  * @see_also: goom
25  *
26  * Monoscope is an audio visualisation element. It creates a coloured
27  * curve of the audio signal like on an oscilloscope.
28  *
29  * <refsect2>
30  * <title>Example launch line</title>
31  * |[
32  * gst-launch-1.0 -v audiotestsrc ! audioconvert ! monoscope ! videoconvert ! ximagesink
33  * ]|
34  * </refsect2>
35  */
36 
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40 
41 #include <gst/video/video.h>
42 #include <gst/audio/audio.h>
43 #include <string.h>
44 #include "gstmonoscope.h"
45 #include "monoscope.h"
46 
47 GST_DEBUG_CATEGORY_STATIC (monoscope_debug);
48 #define GST_CAT_DEFAULT monoscope_debug
49 
50 #if G_BYTE_ORDER == G_BIG_ENDIAN
51 #define RGB_ORDER "xRGB"
52 #else
53 #define RGB_ORDER "BGRx"
54 #endif
55 
56 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
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)
64     );
65 
66 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
67     GST_PAD_SINK,
68     GST_PAD_ALWAYS,
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")
73     );
74 
75 
76 #define gst_monoscope_parent_class parent_class
77 G_DEFINE_TYPE (GstMonoscope, gst_monoscope, GST_TYPE_ELEMENT);
78 
79 static void gst_monoscope_finalize (GObject * object);
80 static GstFlowReturn gst_monoscope_chain (GstPad * pad, GstObject * parent,
81     GstBuffer * buf);
82 static gboolean gst_monoscope_src_setcaps (GstMonoscope * mono, GstCaps * caps);
83 static gboolean gst_monoscope_sink_setcaps (GstMonoscope * mono,
84     GstCaps * caps);
85 static void gst_monoscope_reset (GstMonoscope * monoscope);
86 static gboolean gst_monoscope_sink_event (GstPad * pad, GstObject * parent,
87     GstEvent * event);
88 static gboolean gst_monoscope_src_event (GstPad * pad, GstObject * parent,
89     GstEvent * event);
90 static GstStateChangeReturn gst_monoscope_change_state (GstElement * element,
91     GstStateChange transition);
92 
93 static void
gst_monoscope_class_init(GstMonoscopeClass * klass)94 gst_monoscope_class_init (GstMonoscopeClass * klass)
95 {
96   GObjectClass *gobject_class;
97   GstElementClass *gstelement_class;
98 
99   gobject_class = (GObjectClass *) klass;
100   gstelement_class = (GstElementClass *) klass;
101 
102   gobject_class->finalize = gst_monoscope_finalize;
103 
104   gstelement_class->change_state =
105       GST_DEBUG_FUNCPTR (gst_monoscope_change_state);
106 
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",
110       "Visualization",
111       "Displays a highly stabilised waveform of audio input",
112       "Richard Boulton <richard@tartarus.org>");
113 }
114 
115 static void
gst_monoscope_init(GstMonoscope * monoscope)116 gst_monoscope_init (GstMonoscope * monoscope)
117 {
118   monoscope->sinkpad =
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);
125 
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);
130 
131   monoscope->adapter = gst_adapter_new ();
132   monoscope->next_ts = GST_CLOCK_TIME_NONE;
133   monoscope->bps = sizeof (gint16);
134 
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;
141 
142   /* reset the initial audio state */
143   monoscope->rate = GST_AUDIO_DEF_RATE;
144 }
145 
146 static void
gst_monoscope_finalize(GObject * object)147 gst_monoscope_finalize (GObject * object)
148 {
149   GstMonoscope *monoscope = GST_MONOSCOPE (object);
150 
151   if (monoscope->visstate)
152     monoscope_close (monoscope->visstate);
153 
154   g_object_unref (monoscope->adapter);
155 
156   G_OBJECT_CLASS (parent_class)->finalize (object);
157 }
158 
159 static void
gst_monoscope_reset(GstMonoscope * monoscope)160 gst_monoscope_reset (GstMonoscope * monoscope)
161 {
162   monoscope->next_ts = GST_CLOCK_TIME_NONE;
163 
164   gst_adapter_clear (monoscope->adapter);
165   gst_segment_init (&monoscope->segment, GST_FORMAT_UNDEFINED);
166   monoscope->segment_pending = FALSE;
167 
168   GST_OBJECT_LOCK (monoscope);
169   monoscope->proportion = 1.0;
170   monoscope->earliest_time = -1;
171   GST_OBJECT_UNLOCK (monoscope);
172 }
173 
174 static gboolean
gst_monoscope_sink_setcaps(GstMonoscope * monoscope,GstCaps * caps)175 gst_monoscope_sink_setcaps (GstMonoscope * monoscope, GstCaps * caps)
176 {
177   GstStructure *structure;
178 
179   structure = gst_caps_get_structure (caps, 0);
180 
181   gst_structure_get_int (structure, "rate", &monoscope->rate);
182 
183   GST_DEBUG_OBJECT (monoscope, "sample rate = %d", monoscope->rate);
184   return TRUE;
185 }
186 
187 static gboolean
gst_monoscope_src_setcaps(GstMonoscope * monoscope,GstCaps * caps)188 gst_monoscope_src_setcaps (GstMonoscope * monoscope, GstCaps * caps)
189 {
190   GstStructure *structure;
191   gboolean res;
192 
193   structure = gst_caps_get_structure (caps, 0);
194 
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);
199 
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);
203   monoscope->spf =
204       gst_util_uint64_scale_int (monoscope->rate, monoscope->fps_denom,
205       monoscope->fps_num);
206 
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);
210 
211   if (monoscope->visstate) {
212     monoscope_close (monoscope->visstate);
213     monoscope->visstate = NULL;
214   }
215 
216   monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
217 
218   res = gst_pad_set_caps (monoscope->srcpad, caps);
219 
220   return res && (monoscope->visstate != NULL);
221 }
222 
223 static gboolean
gst_monoscope_src_negotiate(GstMonoscope * monoscope)224 gst_monoscope_src_negotiate (GstMonoscope * monoscope)
225 {
226   GstCaps *othercaps, *target;
227   GstStructure *structure;
228   GstCaps *templ;
229   GstQuery *query;
230   GstBufferPool *pool;
231   GstStructure *config;
232   guint size, min, max;
233 
234   templ = gst_pad_get_pad_template_caps (monoscope->srcpad);
235 
236   GST_DEBUG_OBJECT (monoscope, "performing negotiation");
237 
238   /* see what the peer can do */
239   othercaps = gst_pad_peer_query_caps (monoscope->srcpad, NULL);
240   if (othercaps) {
241     target = gst_caps_intersect (othercaps, templ);
242     gst_caps_unref (othercaps);
243     gst_caps_unref (templ);
244 
245     if (gst_caps_is_empty (target))
246       goto no_format;
247 
248     target = gst_caps_truncate (target);
249   } else {
250     target = templ;
251   }
252 
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);
258   if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
259     gst_structure_fixate_field_nearest_fraction (structure,
260         "pixel-aspect-ratio", 1, 1);
261   target = gst_caps_fixate (target);
262 
263   gst_monoscope_src_setcaps (monoscope, target);
264 
265   /* try to get a bufferpool now */
266   /* find a pool for the negotiated caps now */
267   query = gst_query_new_allocation (target, TRUE);
268 
269   if (!gst_pad_peer_query (monoscope->srcpad, query)) {
270   }
271 
272   if (gst_query_get_n_allocation_pools (query) > 0) {
273     /* we got configuration from our peer, parse them */
274     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
275   } else {
276     pool = NULL;
277     size = monoscope->outsize;
278     min = max = 0;
279   }
280 
281   if (pool == NULL) {
282     /* we did not get a pool, make one ourselves then */
283     pool = gst_buffer_pool_new ();
284   }
285 
286   config = gst_buffer_pool_get_config (pool);
287   gst_buffer_pool_config_set_params (config, target, size, min, max);
288   gst_buffer_pool_set_config (pool, config);
289 
290   if (monoscope->pool) {
291     gst_buffer_pool_set_active (monoscope->pool, TRUE);
292     gst_object_unref (monoscope->pool);
293   }
294   monoscope->pool = pool;
295 
296   /* and activate */
297   gst_buffer_pool_set_active (pool, TRUE);
298 
299   gst_query_unref (query);
300   gst_caps_unref (target);
301 
302   return TRUE;
303 
304 no_format:
305   {
306     gst_caps_unref (target);
307     return FALSE;
308   }
309 }
310 
311 /* make sure we are negotiated */
312 static GstFlowReturn
ensure_negotiated(GstMonoscope * monoscope)313 ensure_negotiated (GstMonoscope * monoscope)
314 {
315   gboolean reconfigure;
316 
317   reconfigure = gst_pad_check_reconfigure (monoscope->srcpad);
318 
319   /* we don't know an output format yet, pick one */
320   if (reconfigure || !gst_pad_has_current_caps (monoscope->srcpad)) {
321     if (!gst_monoscope_src_negotiate (monoscope)) {
322       gst_pad_mark_reconfigure (monoscope->srcpad);
323       if (GST_PAD_IS_FLUSHING (monoscope->srcpad))
324         return GST_FLOW_FLUSHING;
325       else
326         return GST_FLOW_NOT_NEGOTIATED;
327     }
328   }
329   return GST_FLOW_OK;
330 }
331 
332 static GstFlowReturn
gst_monoscope_chain(GstPad * pad,GstObject * parent,GstBuffer * inbuf)333 gst_monoscope_chain (GstPad * pad, GstObject * parent, GstBuffer * inbuf)
334 {
335   GstFlowReturn flow_ret = GST_FLOW_OK;
336   GstMonoscope *monoscope;
337 
338   monoscope = GST_MONOSCOPE (parent);
339 
340   if (monoscope->rate == 0) {
341     gst_buffer_unref (inbuf);
342     flow_ret = GST_FLOW_NOT_NEGOTIATED;
343     goto out;
344   }
345 
346   /* Make sure have an output format */
347   flow_ret = ensure_negotiated (monoscope);
348   if (flow_ret != GST_FLOW_OK) {
349     gst_buffer_unref (inbuf);
350     goto out;
351   }
352 
353   if (monoscope->segment_pending) {
354     gst_pad_push_event (monoscope->srcpad,
355         gst_event_new_segment (&monoscope->segment));
356     monoscope->segment_pending = FALSE;
357   }
358 
359   /* don't try to combine samples from discont buffer */
360   if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DISCONT)) {
361     gst_adapter_clear (monoscope->adapter);
362     monoscope->next_ts = GST_CLOCK_TIME_NONE;
363   }
364 
365   /* Match timestamps from the incoming audio */
366   if (GST_BUFFER_TIMESTAMP (inbuf) != GST_CLOCK_TIME_NONE)
367     monoscope->next_ts = GST_BUFFER_TIMESTAMP (inbuf);
368 
369   GST_LOG_OBJECT (monoscope,
370       "in buffer has %" G_GSIZE_FORMAT " samples, ts=%" GST_TIME_FORMAT,
371       gst_buffer_get_size (inbuf) / monoscope->bps,
372       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
373 
374   gst_adapter_push (monoscope->adapter, inbuf);
375   inbuf = NULL;
376 
377   /* Collect samples until we have enough for an output frame */
378   while (flow_ret == GST_FLOW_OK) {
379     gint16 *samples;
380     GstBuffer *outbuf = NULL;
381     guint32 *pixels, avail, bytesperframe;
382 
383     avail = gst_adapter_available (monoscope->adapter);
384     GST_LOG_OBJECT (monoscope, "bytes avail now %u", avail);
385 
386     bytesperframe = monoscope->spf * monoscope->bps;
387     if (avail < bytesperframe)
388       break;
389 
390     /* FIXME: something is wrong with QoS, we are skipping way too much
391      * stuff even with very low CPU loads */
392 #if 0
393     if (monoscope->next_ts != -1) {
394       gboolean need_skip;
395       gint64 qostime;
396 
397       qostime = gst_segment_to_running_time (&monoscope->segment,
398           GST_FORMAT_TIME, monoscope->next_ts);
399 
400       GST_OBJECT_LOCK (monoscope);
401       /* check for QoS, don't compute buffers that are known to be late */
402       need_skip =
403           GST_CLOCK_TIME_IS_VALID (monoscope->earliest_time) &&
404           qostime <= monoscope->earliest_time;
405       GST_OBJECT_UNLOCK (monoscope);
406 
407       if (need_skip) {
408         GST_WARNING_OBJECT (monoscope,
409             "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
410             GST_TIME_ARGS (qostime), GST_TIME_ARGS (monoscope->earliest_time));
411         goto skip;
412       }
413     }
414 #endif
415 
416     samples = (gint16 *) gst_adapter_map (monoscope->adapter, bytesperframe);
417 
418     if (monoscope->spf < convolver_big) {
419       gint16 in_data[convolver_big], i;
420       gdouble scale = (gdouble) monoscope->spf / (gdouble) convolver_big;
421 
422       for (i = 0; i < convolver_big; ++i) {
423         gdouble off = (gdouble) i * scale;
424         in_data[i] = samples[MIN ((guint) off, monoscope->spf)];
425       }
426       pixels = monoscope_update (monoscope->visstate, in_data);
427     } else {
428       /* not really correct, but looks much prettier */
429       pixels = monoscope_update (monoscope->visstate, samples);
430     }
431 
432     GST_LOG_OBJECT (monoscope, "allocating output buffer");
433     flow_ret = gst_buffer_pool_acquire_buffer (monoscope->pool, &outbuf, NULL);
434     if (flow_ret != GST_FLOW_OK) {
435       gst_adapter_unmap (monoscope->adapter);
436       goto out;
437     }
438 
439     gst_buffer_fill (outbuf, 0, pixels, monoscope->outsize);
440 
441     GST_BUFFER_TIMESTAMP (outbuf) = monoscope->next_ts;
442     GST_BUFFER_DURATION (outbuf) = monoscope->frame_duration;
443 
444     flow_ret = gst_pad_push (monoscope->srcpad, outbuf);
445 
446 #if 0
447   skip:
448 #endif
449 
450     if (GST_CLOCK_TIME_IS_VALID (monoscope->next_ts))
451       monoscope->next_ts += monoscope->frame_duration;
452 
453     gst_adapter_flush (monoscope->adapter, bytesperframe);
454   }
455 
456 out:
457 
458   return flow_ret;
459 }
460 
461 static gboolean
gst_monoscope_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)462 gst_monoscope_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
463 {
464   GstMonoscope *monoscope;
465   gboolean res;
466 
467   monoscope = GST_MONOSCOPE (parent);
468 
469   switch (GST_EVENT_TYPE (event)) {
470     case GST_EVENT_FLUSH_START:
471       res = gst_pad_push_event (monoscope->srcpad, event);
472       break;
473     case GST_EVENT_FLUSH_STOP:
474       gst_monoscope_reset (monoscope);
475       res = gst_pad_push_event (monoscope->srcpad, event);
476       break;
477     case GST_EVENT_SEGMENT:
478     {
479       /* the newsegment values are used to clip the input samples
480        * and to convert the incoming timestamps to running time so
481        * we can do QoS */
482       gst_event_copy_segment (event, &monoscope->segment);
483 
484       /* We forward the event from the chain function after caps are
485        * negotiated. Otherwise we would potentially break the event order and
486        * send the segment event before the caps event */
487       monoscope->segment_pending = TRUE;
488       gst_event_unref (event);
489       res = TRUE;
490       break;
491     }
492     case GST_EVENT_CAPS:
493     {
494       GstCaps *caps;
495 
496       gst_event_parse_caps (event, &caps);
497       gst_monoscope_sink_setcaps (monoscope, caps);
498       gst_event_unref (event);
499       res = TRUE;
500       break;
501     }
502     default:
503       res = gst_pad_push_event (monoscope->srcpad, event);
504       break;
505   }
506 
507   return res;
508 }
509 
510 static gboolean
gst_monoscope_src_event(GstPad * pad,GstObject * parent,GstEvent * event)511 gst_monoscope_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
512 {
513   GstMonoscope *monoscope;
514   gboolean res;
515 
516   monoscope = GST_MONOSCOPE (parent);
517 
518   switch (GST_EVENT_TYPE (event)) {
519     case GST_EVENT_QOS:{
520       gdouble proportion;
521       GstClockTimeDiff diff;
522       GstClockTime timestamp;
523 
524       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
525 
526       /* save stuff for the _chain() function */
527       GST_OBJECT_LOCK (monoscope);
528       monoscope->proportion = proportion;
529       if (diff >= 0)
530         /* we're late, this is a good estimate for next displayable
531          * frame (see part-qos.txt) */
532         monoscope->earliest_time =
533             timestamp + 2 * diff + monoscope->frame_duration;
534       else
535         monoscope->earliest_time = timestamp + diff;
536       GST_OBJECT_UNLOCK (monoscope);
537 
538       res = gst_pad_push_event (monoscope->sinkpad, event);
539       break;
540     }
541     default:
542       res = gst_pad_push_event (monoscope->sinkpad, event);
543       break;
544   }
545 
546   return res;
547 }
548 
549 static GstStateChangeReturn
gst_monoscope_change_state(GstElement * element,GstStateChange transition)550 gst_monoscope_change_state (GstElement * element, GstStateChange transition)
551 {
552   GstMonoscope *monoscope = GST_MONOSCOPE (element);
553   GstStateChangeReturn ret;
554 
555   switch (transition) {
556     case GST_STATE_CHANGE_NULL_TO_READY:
557       break;
558     case GST_STATE_CHANGE_READY_TO_PAUSED:
559       gst_monoscope_reset (monoscope);
560       break;
561     default:
562       break;
563   }
564 
565   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
566 
567   switch (transition) {
568     case GST_STATE_CHANGE_PAUSED_TO_READY:
569       if (monoscope->pool) {
570         gst_buffer_pool_set_active (monoscope->pool, FALSE);
571         gst_object_replace ((GstObject **) & monoscope->pool, NULL);
572       }
573       break;
574     case GST_STATE_CHANGE_READY_TO_NULL:
575       break;
576     default:
577       break;
578   }
579 
580   return ret;
581 }
582 
583 static gboolean
plugin_init(GstPlugin * plugin)584 plugin_init (GstPlugin * plugin)
585 {
586   GST_DEBUG_CATEGORY_INIT (monoscope_debug, "monoscope", 0,
587       "monoscope element");
588 
589   return gst_element_register (plugin, "monoscope",
590       GST_RANK_NONE, GST_TYPE_MONOSCOPE);
591 }
592 
593 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
594     GST_VERSION_MINOR,
595     monoscope,
596     "Monoscope visualization",
597     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
598