aaaa000cdb9d3e3a98d0a77f7e67914fd0b46bd6
[platform/upstream/gstreamer.git] / gst / monoscope / gstmonoscope.c
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, 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 -v audiotestsrc ! audioconvert ! monoscope ! ffmpegcolorspace ! 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 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53 #if G_BYTE_ORDER == G_BIG_ENDIAN
54     GST_STATIC_CAPS ("video/x-raw, "
55         "format = (string) xRGB, "
56         "width = 256, " "height = 128, " "framerate = " GST_VIDEO_FPS_RANGE)
57 #else
58     GST_STATIC_CAPS ("video/x-raw, "
59         "format = (string) BGRx, "
60         "width = 256, " "height = 128, " "framerate = " GST_VIDEO_FPS_RANGE)
61 #endif
62     );
63
64 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS ("audio/x-raw, "
68         "format = (string) " GST_AUDIO_NE (S16) ", "
69         "rate = (int) [ 8000, 96000 ], "
70         "channels = (int) 1, " "layout = (string) interleaved")
71     );
72
73
74 #define gst_monoscope_parent_class parent_class
75 G_DEFINE_TYPE (GstMonoscope, gst_monoscope, GST_TYPE_ELEMENT);
76
77 static void gst_monoscope_finalize (GObject * object);
78 static GstFlowReturn gst_monoscope_chain (GstPad * pad, GstObject * parent,
79     GstBuffer * buf);
80 static gboolean gst_monoscope_src_setcaps (GstMonoscope * mono, GstCaps * caps);
81 static gboolean gst_monoscope_sink_setcaps (GstMonoscope * mono,
82     GstCaps * caps);
83 static void gst_monoscope_reset (GstMonoscope * monoscope);
84 static gboolean gst_monoscope_sink_event (GstPad * pad, GstObject * parent,
85     GstEvent * event);
86 static gboolean gst_monoscope_src_event (GstPad * pad, GstObject * parent,
87     GstEvent * event);
88 static GstStateChangeReturn gst_monoscope_change_state (GstElement * element,
89     GstStateChange transition);
90
91 static void
92 gst_monoscope_class_init (GstMonoscopeClass * klass)
93 {
94   GObjectClass *gobject_class;
95   GstElementClass *gstelement_class;
96
97   gobject_class = (GObjectClass *) klass;
98   gstelement_class = (GstElementClass *) klass;
99
100   gobject_class->finalize = gst_monoscope_finalize;
101
102   gstelement_class->change_state =
103       GST_DEBUG_FUNCPTR (gst_monoscope_change_state);
104
105   gst_element_class_add_pad_template (gstelement_class,
106       gst_static_pad_template_get (&src_template));
107   gst_element_class_add_pad_template (gstelement_class,
108       gst_static_pad_template_get (&sink_template));
109   gst_element_class_set_details_simple (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
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 = 256;
137   monoscope->height = 128;
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
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
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
167   GST_OBJECT_LOCK (monoscope);
168   monoscope->proportion = 1.0;
169   monoscope->earliest_time = -1;
170   GST_OBJECT_UNLOCK (monoscope);
171 }
172
173 static gboolean
174 gst_monoscope_sink_setcaps (GstMonoscope * monoscope, GstCaps * caps)
175 {
176   GstStructure *structure;
177
178   structure = gst_caps_get_structure (caps, 0);
179
180   gst_structure_get_int (structure, "rate", &monoscope->rate);
181
182   GST_DEBUG_OBJECT (monoscope, "sample rate = %d", monoscope->rate);
183   return TRUE;
184 }
185
186 static gboolean
187 gst_monoscope_src_setcaps (GstMonoscope * monoscope, GstCaps * caps)
188 {
189   GstStructure *structure;
190   gboolean res;
191
192   structure = gst_caps_get_structure (caps, 0);
193
194   gst_structure_get_int (structure, "width", &monoscope->width);
195   gst_structure_get_int (structure, "height", &monoscope->height);
196   gst_structure_get_fraction (structure, "framerate", &monoscope->fps_num,
197       &monoscope->fps_denom);
198
199   monoscope->outsize = monoscope->width * monoscope->height * 4;
200   monoscope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
201       monoscope->fps_denom, monoscope->fps_num);
202   monoscope->spf =
203       gst_util_uint64_scale_int (monoscope->rate, monoscope->fps_denom,
204       monoscope->fps_num);
205
206   GST_DEBUG_OBJECT (monoscope, "dimension %dx%d, framerate %d/%d, spf %d",
207       monoscope->width, monoscope->height, monoscope->fps_num,
208       monoscope->fps_denom, monoscope->spf);
209
210   if (monoscope->visstate) {
211     monoscope_close (monoscope->visstate);
212     monoscope->visstate = NULL;
213   }
214
215   monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
216
217   res = gst_pad_push_event (monoscope->srcpad, gst_event_new_caps (caps));
218
219   return res && (monoscope->visstate != NULL);
220 }
221
222 static gboolean
223 gst_monoscope_src_negotiate (GstMonoscope * monoscope)
224 {
225   GstCaps *othercaps, *target;
226   GstStructure *structure;
227   GstCaps *templ;
228   GstQuery *query;
229   GstBufferPool *pool = NULL;
230   guint size, min, max, prefix, alignment;
231
232   templ = gst_pad_get_pad_template_caps (monoscope->srcpad);
233
234   GST_DEBUG_OBJECT (monoscope, "performing negotiation");
235
236   /* see what the peer can do */
237   othercaps = gst_pad_peer_query_caps (monoscope->srcpad, NULL);
238   if (othercaps) {
239     target = gst_caps_intersect (othercaps, templ);
240     gst_caps_unref (othercaps);
241     gst_caps_unref (templ);
242
243     if (gst_caps_is_empty (target))
244       goto no_format;
245
246     target = gst_caps_truncate (target);
247   } else {
248     target = templ;
249   }
250
251   target = gst_caps_make_writable (target);
252   structure = gst_caps_get_structure (target, 0);
253   gst_structure_fixate_field_nearest_int (structure, "width", 320);
254   gst_structure_fixate_field_nearest_int (structure, "height", 240);
255   gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
256
257   gst_monoscope_src_setcaps (monoscope, target);
258
259   /* try to get a bufferpool now */
260   /* find a pool for the negotiated caps now */
261   query = gst_query_new_allocation (target, TRUE);
262
263   if (gst_pad_peer_query (monoscope->srcpad, query)) {
264     /* we got configuration from our peer, parse them */
265     gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
266         &alignment, &pool);
267   } else {
268     size = monoscope->outsize;
269     min = max = 0;
270     prefix = 0;
271     alignment = 0;
272   }
273
274   if (pool == NULL) {
275     GstStructure *config;
276
277     /* we did not get a pool, make one ourselves then */
278     pool = gst_buffer_pool_new ();
279
280     config = gst_buffer_pool_get_config (pool);
281     gst_buffer_pool_config_set (config, target, size, min, max, prefix,
282         alignment);
283     gst_buffer_pool_set_config (pool, config);
284   }
285
286   if (monoscope->pool)
287     gst_object_unref (monoscope->pool);
288   monoscope->pool = pool;
289
290   /* and activate */
291   gst_buffer_pool_set_active (pool, TRUE);
292
293   gst_caps_unref (target);
294
295   return TRUE;
296
297 no_format:
298   {
299     gst_caps_unref (target);
300     return FALSE;
301   }
302 }
303
304 /* make sure we are negotiated */
305 static GstFlowReturn
306 ensure_negotiated (GstMonoscope * monoscope)
307 {
308   gboolean reconfigure;
309
310   reconfigure = gst_pad_check_reconfigure (monoscope->srcpad);
311
312   /* we don't know an output format yet, pick one */
313   if (reconfigure || !gst_pad_has_current_caps (monoscope->srcpad)) {
314     if (!gst_monoscope_src_negotiate (monoscope))
315       return GST_FLOW_NOT_NEGOTIATED;
316   }
317   return GST_FLOW_OK;
318 }
319
320 static GstFlowReturn
321 gst_monoscope_chain (GstPad * pad, GstObject * parent, GstBuffer * inbuf)
322 {
323   GstFlowReturn flow_ret = GST_FLOW_OK;
324   GstMonoscope *monoscope;
325
326   monoscope = GST_MONOSCOPE (parent);
327
328   if (monoscope->rate == 0) {
329     gst_buffer_unref (inbuf);
330     flow_ret = GST_FLOW_NOT_NEGOTIATED;
331     goto out;
332   }
333
334   /* Make sure have an output format */
335   flow_ret = ensure_negotiated (monoscope);
336   if (flow_ret != GST_FLOW_OK) {
337     gst_buffer_unref (inbuf);
338     goto out;
339   }
340
341   /* don't try to combine samples from discont buffer */
342   if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DISCONT)) {
343     gst_adapter_clear (monoscope->adapter);
344     monoscope->next_ts = GST_CLOCK_TIME_NONE;
345   }
346
347   /* Match timestamps from the incoming audio */
348   if (GST_BUFFER_TIMESTAMP (inbuf) != GST_CLOCK_TIME_NONE)
349     monoscope->next_ts = GST_BUFFER_TIMESTAMP (inbuf);
350
351   GST_LOG_OBJECT (monoscope, "in buffer has %d samples, ts=%" GST_TIME_FORMAT,
352       gst_buffer_get_size (inbuf) / monoscope->bps,
353       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
354
355   gst_adapter_push (monoscope->adapter, inbuf);
356   inbuf = NULL;
357
358   /* Collect samples until we have enough for an output frame */
359   while (flow_ret == GST_FLOW_OK) {
360     gint16 *samples;
361     GstBuffer *outbuf = NULL;
362     guint32 *pixels, avail, bytesperframe;
363
364     avail = gst_adapter_available (monoscope->adapter);
365     GST_LOG_OBJECT (monoscope, "bytes avail now %u", avail);
366
367     bytesperframe = monoscope->spf * monoscope->bps;
368     if (avail < bytesperframe)
369       break;
370
371     /* FIXME: something is wrong with QoS, we are skipping way too much
372      * stuff even with very low CPU loads */
373 #if 0
374     if (monoscope->next_ts != -1) {
375       gboolean need_skip;
376       gint64 qostime;
377
378       qostime = gst_segment_to_running_time (&monoscope->segment,
379           GST_FORMAT_TIME, monoscope->next_ts);
380
381       GST_OBJECT_LOCK (monoscope);
382       /* check for QoS, don't compute buffers that are known to be late */
383       need_skip =
384           GST_CLOCK_TIME_IS_VALID (monoscope->earliest_time) &&
385           qostime <= monoscope->earliest_time;
386       GST_OBJECT_UNLOCK (monoscope);
387
388       if (need_skip) {
389         GST_WARNING_OBJECT (monoscope,
390             "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
391             GST_TIME_ARGS (qostime), GST_TIME_ARGS (monoscope->earliest_time));
392         goto skip;
393       }
394     }
395 #endif
396
397     samples = (gint16 *) gst_adapter_map (monoscope->adapter, bytesperframe);
398
399     if (monoscope->spf < 512) {
400       gint16 in_data[512], i;
401
402       for (i = 0; i < 512; ++i) {
403         gdouble off;
404
405         off = ((gdouble) i * (gdouble) monoscope->spf) / 512.0;
406         in_data[i] = samples[MIN ((guint) off, monoscope->spf)];
407       }
408       pixels = monoscope_update (monoscope->visstate, in_data);
409     } else {
410       /* not really correct, but looks much prettier */
411       pixels = monoscope_update (monoscope->visstate, samples);
412     }
413
414     GST_LOG_OBJECT (monoscope, "allocating output buffer");
415     flow_ret = gst_buffer_pool_acquire_buffer (monoscope->pool, &outbuf, NULL);
416     if (flow_ret != GST_FLOW_OK) {
417       gst_adapter_unmap (monoscope->adapter);
418       goto out;
419     }
420
421     gst_buffer_fill (outbuf, 0, pixels, monoscope->outsize);
422
423     GST_BUFFER_TIMESTAMP (outbuf) = monoscope->next_ts;
424     GST_BUFFER_DURATION (outbuf) = monoscope->frame_duration;
425
426     flow_ret = gst_pad_push (monoscope->srcpad, outbuf);
427
428 #if 0
429   skip:
430 #endif
431
432     if (GST_CLOCK_TIME_IS_VALID (monoscope->next_ts))
433       monoscope->next_ts += monoscope->frame_duration;
434
435     gst_adapter_flush (monoscope->adapter, bytesperframe);
436   }
437
438 out:
439
440   return flow_ret;
441 }
442
443 static gboolean
444 gst_monoscope_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
445 {
446   GstMonoscope *monoscope;
447   gboolean res;
448
449   monoscope = GST_MONOSCOPE (parent);
450
451   switch (GST_EVENT_TYPE (event)) {
452     case GST_EVENT_FLUSH_START:
453       res = gst_pad_push_event (monoscope->srcpad, event);
454       break;
455     case GST_EVENT_FLUSH_STOP:
456       gst_monoscope_reset (monoscope);
457       res = gst_pad_push_event (monoscope->srcpad, event);
458       break;
459     case GST_EVENT_SEGMENT:
460     {
461       /* the newsegment values are used to clip the input samples
462        * and to convert the incomming timestamps to running time so
463        * we can do QoS */
464       gst_event_copy_segment (event, &monoscope->segment);
465
466       res = gst_pad_push_event (monoscope->srcpad, event);
467       break;
468     }
469     case GST_EVENT_CAPS:
470     {
471       GstCaps *caps;
472
473       gst_event_parse_caps (event, &caps);
474       gst_monoscope_sink_setcaps (monoscope, caps);
475       gst_event_unref (event);
476       res = TRUE;
477       break;
478     }
479     default:
480       res = gst_pad_push_event (monoscope->srcpad, event);
481       break;
482   }
483
484   return res;
485 }
486
487 static gboolean
488 gst_monoscope_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
489 {
490   GstMonoscope *monoscope;
491   gboolean res;
492
493   monoscope = GST_MONOSCOPE (parent);
494
495   switch (GST_EVENT_TYPE (event)) {
496     case GST_EVENT_QOS:{
497       gdouble proportion;
498       GstClockTimeDiff diff;
499       GstClockTime timestamp;
500
501       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
502
503       /* save stuff for the _chain() function */
504       GST_OBJECT_LOCK (monoscope);
505       monoscope->proportion = proportion;
506       if (diff >= 0)
507         /* we're late, this is a good estimate for next displayable
508          * frame (see part-qos.txt) */
509         monoscope->earliest_time =
510             timestamp + 2 * diff + monoscope->frame_duration;
511       else
512         monoscope->earliest_time = timestamp + diff;
513       GST_OBJECT_UNLOCK (monoscope);
514
515       res = gst_pad_push_event (monoscope->sinkpad, event);
516       break;
517     }
518     default:
519       res = gst_pad_push_event (monoscope->sinkpad, event);
520       break;
521   }
522
523   return res;
524 }
525
526 static GstStateChangeReturn
527 gst_monoscope_change_state (GstElement * element, GstStateChange transition)
528 {
529   GstMonoscope *monoscope = GST_MONOSCOPE (element);
530   GstStateChangeReturn ret;
531
532   switch (transition) {
533     case GST_STATE_CHANGE_NULL_TO_READY:
534       break;
535     case GST_STATE_CHANGE_READY_TO_PAUSED:
536       gst_monoscope_reset (monoscope);
537       break;
538     default:
539       break;
540   }
541
542   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
543
544   switch (transition) {
545     case GST_STATE_CHANGE_PAUSED_TO_READY:
546       if (monoscope->pool) {
547         gst_buffer_pool_set_active (monoscope->pool, FALSE);
548         gst_object_replace ((GstObject **) & monoscope->pool, NULL);
549       }
550       break;
551     case GST_STATE_CHANGE_READY_TO_NULL:
552       break;
553     default:
554       break;
555   }
556
557   return ret;
558 }
559
560 static gboolean
561 plugin_init (GstPlugin * plugin)
562 {
563   GST_DEBUG_CATEGORY_INIT (monoscope_debug, "monoscope", 0,
564       "monoscope element");
565
566   return gst_element_register (plugin, "monoscope",
567       GST_RANK_NONE, GST_TYPE_MONOSCOPE);
568 }
569
570 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
571     GST_VERSION_MINOR,
572     "monoscope",
573     "Monoscope visualization",
574     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);