gst Factor out endian-order RGB formats
[platform/upstream/gst-plugins-good.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., 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
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_pad_template (gstelement_class,
108       gst_static_pad_template_get (&src_template));
109   gst_element_class_add_pad_template (gstelement_class,
110       gst_static_pad_template_get (&sink_template));
111   gst_element_class_set_static_metadata (gstelement_class, "Monoscope",
112       "Visualization",
113       "Displays a highly stabilised waveform of audio input",
114       "Richard Boulton <richard@tartarus.org>");
115 }
116
117 static void
118 gst_monoscope_init (GstMonoscope * monoscope)
119 {
120   monoscope->sinkpad =
121       gst_pad_new_from_static_template (&sink_template, "sink");
122   gst_pad_set_chain_function (monoscope->sinkpad,
123       GST_DEBUG_FUNCPTR (gst_monoscope_chain));
124   gst_pad_set_event_function (monoscope->sinkpad,
125       GST_DEBUG_FUNCPTR (gst_monoscope_sink_event));
126   gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
127
128   monoscope->srcpad = gst_pad_new_from_static_template (&src_template, "src");
129   gst_pad_set_event_function (monoscope->srcpad,
130       GST_DEBUG_FUNCPTR (gst_monoscope_src_event));
131   gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
132
133   monoscope->adapter = gst_adapter_new ();
134   monoscope->next_ts = GST_CLOCK_TIME_NONE;
135   monoscope->bps = sizeof (gint16);
136
137   /* reset the initial video state */
138   monoscope->width = scope_width;
139   monoscope->height = scope_height;
140   monoscope->fps_num = 25;      /* desired frame rate */
141   monoscope->fps_denom = 1;
142   monoscope->visstate = NULL;
143
144   /* reset the initial audio state */
145   monoscope->rate = GST_AUDIO_DEF_RATE;
146 }
147
148 static void
149 gst_monoscope_finalize (GObject * object)
150 {
151   GstMonoscope *monoscope = GST_MONOSCOPE (object);
152
153   if (monoscope->visstate)
154     monoscope_close (monoscope->visstate);
155
156   g_object_unref (monoscope->adapter);
157
158   G_OBJECT_CLASS (parent_class)->finalize (object);
159 }
160
161 static void
162 gst_monoscope_reset (GstMonoscope * monoscope)
163 {
164   monoscope->next_ts = GST_CLOCK_TIME_NONE;
165
166   gst_adapter_clear (monoscope->adapter);
167   gst_segment_init (&monoscope->segment, GST_FORMAT_UNDEFINED);
168
169   GST_OBJECT_LOCK (monoscope);
170   monoscope->proportion = 1.0;
171   monoscope->earliest_time = -1;
172   GST_OBJECT_UNLOCK (monoscope);
173 }
174
175 static gboolean
176 gst_monoscope_sink_setcaps (GstMonoscope * monoscope, GstCaps * caps)
177 {
178   GstStructure *structure;
179
180   structure = gst_caps_get_structure (caps, 0);
181
182   gst_structure_get_int (structure, "rate", &monoscope->rate);
183
184   GST_DEBUG_OBJECT (monoscope, "sample rate = %d", monoscope->rate);
185   return TRUE;
186 }
187
188 static gboolean
189 gst_monoscope_src_setcaps (GstMonoscope * monoscope, GstCaps * caps)
190 {
191   GstStructure *structure;
192   gboolean res;
193
194   structure = gst_caps_get_structure (caps, 0);
195
196   gst_structure_get_int (structure, "width", &monoscope->width);
197   gst_structure_get_int (structure, "height", &monoscope->height);
198   gst_structure_get_fraction (structure, "framerate", &monoscope->fps_num,
199       &monoscope->fps_denom);
200
201   monoscope->outsize = monoscope->width * monoscope->height * 4;
202   monoscope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
203       monoscope->fps_denom, monoscope->fps_num);
204   monoscope->spf =
205       gst_util_uint64_scale_int (monoscope->rate, monoscope->fps_denom,
206       monoscope->fps_num);
207
208   GST_DEBUG_OBJECT (monoscope, "dimension %dx%d, framerate %d/%d, spf %d",
209       monoscope->width, monoscope->height, monoscope->fps_num,
210       monoscope->fps_denom, monoscope->spf);
211
212   if (monoscope->visstate) {
213     monoscope_close (monoscope->visstate);
214     monoscope->visstate = NULL;
215   }
216
217   monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
218
219   res = gst_pad_set_caps (monoscope->srcpad, caps);
220
221   return res && (monoscope->visstate != NULL);
222 }
223
224 static gboolean
225 gst_monoscope_src_negotiate (GstMonoscope * monoscope)
226 {
227   GstCaps *othercaps, *target;
228   GstStructure *structure;
229   GstCaps *templ;
230   GstQuery *query;
231   GstBufferPool *pool;
232   GstStructure *config;
233   guint size, min, max;
234
235   templ = gst_pad_get_pad_template_caps (monoscope->srcpad);
236
237   GST_DEBUG_OBJECT (monoscope, "performing negotiation");
238
239   /* see what the peer can do */
240   othercaps = gst_pad_peer_query_caps (monoscope->srcpad, NULL);
241   if (othercaps) {
242     target = gst_caps_intersect (othercaps, templ);
243     gst_caps_unref (othercaps);
244     gst_caps_unref (templ);
245
246     if (gst_caps_is_empty (target))
247       goto no_format;
248
249     target = gst_caps_truncate (target);
250   } else {
251     target = templ;
252   }
253
254   target = gst_caps_make_writable (target);
255   structure = gst_caps_get_structure (target, 0);
256   gst_structure_fixate_field_nearest_int (structure, "width", 320);
257   gst_structure_fixate_field_nearest_int (structure, "height", 240);
258   gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
259
260   gst_monoscope_src_setcaps (monoscope, target);
261
262   /* try to get a bufferpool now */
263   /* find a pool for the negotiated caps now */
264   query = gst_query_new_allocation (target, TRUE);
265
266   if (!gst_pad_peer_query (monoscope->srcpad, query)) {
267   }
268
269   if (gst_query_get_n_allocation_pools (query) > 0) {
270     /* we got configuration from our peer, parse them */
271     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
272   } else {
273     pool = NULL;
274     size = monoscope->outsize;
275     min = max = 0;
276   }
277
278   if (pool == NULL) {
279     /* we did not get a pool, make one ourselves then */
280     pool = gst_buffer_pool_new ();
281   }
282
283   config = gst_buffer_pool_get_config (pool);
284   gst_buffer_pool_config_set_params (config, target, size, min, max);
285   gst_buffer_pool_set_config (pool, config);
286
287   if (monoscope->pool) {
288     gst_buffer_pool_set_active (monoscope->pool, TRUE);
289     gst_object_unref (monoscope->pool);
290   }
291   monoscope->pool = pool;
292
293   /* and activate */
294   gst_buffer_pool_set_active (pool, TRUE);
295
296   gst_caps_unref (target);
297
298   return TRUE;
299
300 no_format:
301   {
302     gst_caps_unref (target);
303     return FALSE;
304   }
305 }
306
307 /* make sure we are negotiated */
308 static GstFlowReturn
309 ensure_negotiated (GstMonoscope * monoscope)
310 {
311   gboolean reconfigure;
312
313   reconfigure = gst_pad_check_reconfigure (monoscope->srcpad);
314
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       return GST_FLOW_NOT_NEGOTIATED;
319   }
320   return GST_FLOW_OK;
321 }
322
323 static GstFlowReturn
324 gst_monoscope_chain (GstPad * pad, GstObject * parent, GstBuffer * inbuf)
325 {
326   GstFlowReturn flow_ret = GST_FLOW_OK;
327   GstMonoscope *monoscope;
328
329   monoscope = GST_MONOSCOPE (parent);
330
331   if (monoscope->rate == 0) {
332     gst_buffer_unref (inbuf);
333     flow_ret = GST_FLOW_NOT_NEGOTIATED;
334     goto out;
335   }
336
337   /* Make sure have an output format */
338   flow_ret = ensure_negotiated (monoscope);
339   if (flow_ret != GST_FLOW_OK) {
340     gst_buffer_unref (inbuf);
341     goto out;
342   }
343
344   /* don't try to combine samples from discont buffer */
345   if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DISCONT)) {
346     gst_adapter_clear (monoscope->adapter);
347     monoscope->next_ts = GST_CLOCK_TIME_NONE;
348   }
349
350   /* Match timestamps from the incoming audio */
351   if (GST_BUFFER_TIMESTAMP (inbuf) != GST_CLOCK_TIME_NONE)
352     monoscope->next_ts = GST_BUFFER_TIMESTAMP (inbuf);
353
354   GST_LOG_OBJECT (monoscope,
355       "in buffer has %" G_GSIZE_FORMAT " samples, ts=%" GST_TIME_FORMAT,
356       gst_buffer_get_size (inbuf) / monoscope->bps,
357       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
358
359   gst_adapter_push (monoscope->adapter, inbuf);
360   inbuf = NULL;
361
362   /* Collect samples until we have enough for an output frame */
363   while (flow_ret == GST_FLOW_OK) {
364     gint16 *samples;
365     GstBuffer *outbuf = NULL;
366     guint32 *pixels, avail, bytesperframe;
367
368     avail = gst_adapter_available (monoscope->adapter);
369     GST_LOG_OBJECT (monoscope, "bytes avail now %u", avail);
370
371     bytesperframe = monoscope->spf * monoscope->bps;
372     if (avail < bytesperframe)
373       break;
374
375     /* FIXME: something is wrong with QoS, we are skipping way too much
376      * stuff even with very low CPU loads */
377 #if 0
378     if (monoscope->next_ts != -1) {
379       gboolean need_skip;
380       gint64 qostime;
381
382       qostime = gst_segment_to_running_time (&monoscope->segment,
383           GST_FORMAT_TIME, monoscope->next_ts);
384
385       GST_OBJECT_LOCK (monoscope);
386       /* check for QoS, don't compute buffers that are known to be late */
387       need_skip =
388           GST_CLOCK_TIME_IS_VALID (monoscope->earliest_time) &&
389           qostime <= monoscope->earliest_time;
390       GST_OBJECT_UNLOCK (monoscope);
391
392       if (need_skip) {
393         GST_WARNING_OBJECT (monoscope,
394             "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
395             GST_TIME_ARGS (qostime), GST_TIME_ARGS (monoscope->earliest_time));
396         goto skip;
397       }
398     }
399 #endif
400
401     samples = (gint16 *) gst_adapter_map (monoscope->adapter, bytesperframe);
402
403     if (monoscope->spf < convolver_big) {
404       gint16 in_data[convolver_big], i;
405       gdouble scale = (gdouble) monoscope->spf / (gdouble) convolver_big;
406
407       for (i = 0; i < convolver_big; ++i) {
408         gdouble off = (gdouble) i * scale;
409         in_data[i] = samples[MIN ((guint) off, monoscope->spf)];
410       }
411       pixels = monoscope_update (monoscope->visstate, in_data);
412     } else {
413       /* not really correct, but looks much prettier */
414       pixels = monoscope_update (monoscope->visstate, samples);
415     }
416
417     GST_LOG_OBJECT (monoscope, "allocating output buffer");
418     flow_ret = gst_buffer_pool_acquire_buffer (monoscope->pool, &outbuf, NULL);
419     if (flow_ret != GST_FLOW_OK) {
420       gst_adapter_unmap (monoscope->adapter);
421       goto out;
422     }
423
424     gst_buffer_fill (outbuf, 0, pixels, monoscope->outsize);
425
426     GST_BUFFER_TIMESTAMP (outbuf) = monoscope->next_ts;
427     GST_BUFFER_DURATION (outbuf) = monoscope->frame_duration;
428
429     flow_ret = gst_pad_push (monoscope->srcpad, outbuf);
430
431 #if 0
432   skip:
433 #endif
434
435     if (GST_CLOCK_TIME_IS_VALID (monoscope->next_ts))
436       monoscope->next_ts += monoscope->frame_duration;
437
438     gst_adapter_flush (monoscope->adapter, bytesperframe);
439   }
440
441 out:
442
443   return flow_ret;
444 }
445
446 static gboolean
447 gst_monoscope_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
448 {
449   GstMonoscope *monoscope;
450   gboolean res;
451
452   monoscope = GST_MONOSCOPE (parent);
453
454   switch (GST_EVENT_TYPE (event)) {
455     case GST_EVENT_FLUSH_START:
456       res = gst_pad_push_event (monoscope->srcpad, event);
457       break;
458     case GST_EVENT_FLUSH_STOP:
459       gst_monoscope_reset (monoscope);
460       res = gst_pad_push_event (monoscope->srcpad, event);
461       break;
462     case GST_EVENT_SEGMENT:
463     {
464       /* the newsegment values are used to clip the input samples
465        * and to convert the incomming timestamps to running time so
466        * we can do QoS */
467       gst_event_copy_segment (event, &monoscope->segment);
468
469       res = gst_pad_push_event (monoscope->srcpad, event);
470       break;
471     }
472     case GST_EVENT_CAPS:
473     {
474       GstCaps *caps;
475
476       gst_event_parse_caps (event, &caps);
477       gst_monoscope_sink_setcaps (monoscope, caps);
478       gst_event_unref (event);
479       res = TRUE;
480       break;
481     }
482     default:
483       res = gst_pad_push_event (monoscope->srcpad, event);
484       break;
485   }
486
487   return res;
488 }
489
490 static gboolean
491 gst_monoscope_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
492 {
493   GstMonoscope *monoscope;
494   gboolean res;
495
496   monoscope = GST_MONOSCOPE (parent);
497
498   switch (GST_EVENT_TYPE (event)) {
499     case GST_EVENT_QOS:{
500       gdouble proportion;
501       GstClockTimeDiff diff;
502       GstClockTime timestamp;
503
504       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
505
506       /* save stuff for the _chain() function */
507       GST_OBJECT_LOCK (monoscope);
508       monoscope->proportion = proportion;
509       if (diff >= 0)
510         /* we're late, this is a good estimate for next displayable
511          * frame (see part-qos.txt) */
512         monoscope->earliest_time =
513             timestamp + 2 * diff + monoscope->frame_duration;
514       else
515         monoscope->earliest_time = timestamp + diff;
516       GST_OBJECT_UNLOCK (monoscope);
517
518       res = gst_pad_push_event (monoscope->sinkpad, event);
519       break;
520     }
521     default:
522       res = gst_pad_push_event (monoscope->sinkpad, event);
523       break;
524   }
525
526   return res;
527 }
528
529 static GstStateChangeReturn
530 gst_monoscope_change_state (GstElement * element, GstStateChange transition)
531 {
532   GstMonoscope *monoscope = GST_MONOSCOPE (element);
533   GstStateChangeReturn ret;
534
535   switch (transition) {
536     case GST_STATE_CHANGE_NULL_TO_READY:
537       break;
538     case GST_STATE_CHANGE_READY_TO_PAUSED:
539       gst_monoscope_reset (monoscope);
540       break;
541     default:
542       break;
543   }
544
545   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
546
547   switch (transition) {
548     case GST_STATE_CHANGE_PAUSED_TO_READY:
549       if (monoscope->pool) {
550         gst_buffer_pool_set_active (monoscope->pool, FALSE);
551         gst_object_replace ((GstObject **) & monoscope->pool, NULL);
552       }
553       break;
554     case GST_STATE_CHANGE_READY_TO_NULL:
555       break;
556     default:
557       break;
558   }
559
560   return ret;
561 }
562
563 static gboolean
564 plugin_init (GstPlugin * plugin)
565 {
566   GST_DEBUG_CATEGORY_INIT (monoscope_debug, "monoscope", 0,
567       "monoscope element");
568
569   return gst_element_register (plugin, "monoscope",
570       GST_RANK_NONE, GST_TYPE_MONOSCOPE);
571 }
572
573 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
574     GST_VERSION_MINOR,
575     monoscope,
576     "Monoscope visualization",
577     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);