Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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 #if G_BYTE_ORDER == G_BIG_ENDIAN
51 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("video/x-raw-rgb, "
55         "bpp = (int) 32, "
56         "depth = (int) 24, "
57         "endianness = (int) BIG_ENDIAN, "
58         "red_mask = (int) " GST_VIDEO_BYTE2_MASK_32 ", "
59         "green_mask = (int) " GST_VIDEO_BYTE3_MASK_32 ", "
60         "blue_mask = (int) " GST_VIDEO_BYTE4_MASK_32 ", "
61         "width = (int)256, "
62         "height = (int)128, " "framerate = " GST_VIDEO_FPS_RANGE)
63     );
64 #else
65 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
66     GST_PAD_SRC,
67     GST_PAD_ALWAYS,
68     GST_STATIC_CAPS ("video/x-raw-rgb, "
69         "bpp = (int) 32, "
70         "depth = (int) 24, "
71         "endianness = (int) BIG_ENDIAN, "
72         "red_mask = (int) " GST_VIDEO_BYTE3_MASK_32 ", "
73         "green_mask = (int) " GST_VIDEO_BYTE2_MASK_32 ", "
74         "blue_mask = (int) " GST_VIDEO_BYTE1_MASK_32 ", "
75         "width = (int)256, "
76         "height = (int)128, " "framerate = " GST_VIDEO_FPS_RANGE)
77     );
78 #endif
79
80 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
81     GST_PAD_SINK,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS (GST_AUDIO_INT_STANDARD_PAD_TEMPLATE_CAPS)
84     );
85
86
87 GST_BOILERPLATE (GstMonoscope, gst_monoscope, GstElement, GST_TYPE_ELEMENT);
88
89 static void gst_monoscope_finalize (GObject * object);
90 static GstFlowReturn gst_monoscope_chain (GstPad * pad, GstBuffer * buf);
91 static gboolean gst_monoscope_src_setcaps (GstPad * pad, GstCaps * caps);
92 static gboolean gst_monoscope_sink_setcaps (GstPad * pad, GstCaps * caps);
93 static void gst_monoscope_reset (GstMonoscope * monoscope);
94 static gboolean gst_monoscope_sink_event (GstPad * pad, GstEvent * event);
95 static gboolean gst_monoscope_src_event (GstPad * pad, GstEvent * event);
96 static GstStateChangeReturn gst_monoscope_change_state (GstElement * element,
97     GstStateChange transition);
98
99 static void
100 gst_monoscope_base_init (gpointer klass)
101 {
102   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
103
104   gst_element_class_add_static_pad_template (element_class, &src_template);
105   gst_element_class_add_static_pad_template (element_class,
106       &sink_template);
107   gst_element_class_set_details_simple (element_class, "Monoscope",
108       "Visualization",
109       "Displays a highly stabilised waveform of audio input",
110       "Richard Boulton <richard@tartarus.org>");
111 }
112
113 static void
114 gst_monoscope_class_init (GstMonoscopeClass * klass)
115 {
116   GObjectClass *gobject_class;
117   GstElementClass *gstelement_class;
118
119   gobject_class = (GObjectClass *) klass;
120   gstelement_class = (GstElementClass *) klass;
121
122   gobject_class->finalize = gst_monoscope_finalize;
123
124   gstelement_class->change_state =
125       GST_DEBUG_FUNCPTR (gst_monoscope_change_state);
126 }
127
128 static void
129 gst_monoscope_init (GstMonoscope * monoscope, GstMonoscopeClass * klass)
130 {
131   monoscope->sinkpad =
132       gst_pad_new_from_static_template (&sink_template, "sink");
133   gst_pad_set_chain_function (monoscope->sinkpad,
134       GST_DEBUG_FUNCPTR (gst_monoscope_chain));
135   gst_pad_set_event_function (monoscope->sinkpad,
136       GST_DEBUG_FUNCPTR (gst_monoscope_sink_event));
137   gst_pad_set_setcaps_function (monoscope->sinkpad,
138       GST_DEBUG_FUNCPTR (gst_monoscope_sink_setcaps));
139   gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->sinkpad);
140
141   monoscope->srcpad = gst_pad_new_from_static_template (&src_template, "src");
142   gst_pad_set_setcaps_function (monoscope->srcpad,
143       GST_DEBUG_FUNCPTR (gst_monoscope_src_setcaps));
144   gst_pad_set_event_function (monoscope->srcpad,
145       GST_DEBUG_FUNCPTR (gst_monoscope_src_event));
146   gst_element_add_pad (GST_ELEMENT (monoscope), monoscope->srcpad);
147
148   monoscope->adapter = gst_adapter_new ();
149   monoscope->next_ts = GST_CLOCK_TIME_NONE;
150   monoscope->bps = sizeof (gint16);
151
152   /* reset the initial video state */
153   monoscope->width = 256;
154   monoscope->height = 128;
155   monoscope->fps_num = 25;      /* desired frame rate */
156   monoscope->fps_denom = 1;
157   monoscope->visstate = NULL;
158
159   /* reset the initial audio state */
160   monoscope->rate = GST_AUDIO_DEF_RATE;
161 }
162
163 static void
164 gst_monoscope_finalize (GObject * object)
165 {
166   GstMonoscope *monoscope = GST_MONOSCOPE (object);
167
168   if (monoscope->visstate)
169     monoscope_close (monoscope->visstate);
170
171   g_object_unref (monoscope->adapter);
172
173   G_OBJECT_CLASS (parent_class)->finalize (object);
174 }
175
176 static void
177 gst_monoscope_reset (GstMonoscope * monoscope)
178 {
179   monoscope->next_ts = GST_CLOCK_TIME_NONE;
180
181   gst_adapter_clear (monoscope->adapter);
182   gst_segment_init (&monoscope->segment, GST_FORMAT_UNDEFINED);
183
184   GST_OBJECT_LOCK (monoscope);
185   monoscope->proportion = 1.0;
186   monoscope->earliest_time = -1;
187   GST_OBJECT_UNLOCK (monoscope);
188 }
189
190 static gboolean
191 gst_monoscope_sink_setcaps (GstPad * pad, GstCaps * caps)
192 {
193   GstMonoscope *monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
194   GstStructure *structure;
195
196   structure = gst_caps_get_structure (caps, 0);
197
198   gst_structure_get_int (structure, "rate", &monoscope->rate);
199
200   GST_DEBUG_OBJECT (monoscope, "sample rate = %d", monoscope->rate);
201   return TRUE;
202 }
203
204 static gboolean
205 gst_monoscope_src_setcaps (GstPad * pad, GstCaps * caps)
206 {
207   GstMonoscope *monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
208   GstStructure *structure;
209
210   structure = gst_caps_get_structure (caps, 0);
211
212   gst_structure_get_int (structure, "width", &monoscope->width);
213   gst_structure_get_int (structure, "height", &monoscope->height);
214   gst_structure_get_fraction (structure, "framerate", &monoscope->fps_num,
215       &monoscope->fps_denom);
216
217   monoscope->outsize = monoscope->width * monoscope->height * 4;
218   monoscope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
219       monoscope->fps_denom, monoscope->fps_num);
220   monoscope->spf =
221       gst_util_uint64_scale_int (monoscope->rate, monoscope->fps_denom,
222       monoscope->fps_num);
223
224   GST_DEBUG_OBJECT (monoscope, "dimension %dx%d, framerate %d/%d, spf %d",
225       monoscope->width, monoscope->height, monoscope->fps_num,
226       monoscope->fps_denom, monoscope->spf);
227
228   if (monoscope->visstate) {
229     monoscope_close (monoscope->visstate);
230     monoscope->visstate = NULL;
231   }
232
233   monoscope->visstate = monoscope_init (monoscope->width, monoscope->height);
234
235   return (monoscope->visstate != NULL);
236 }
237
238 static gboolean
239 gst_monoscope_src_negotiate (GstMonoscope * monoscope)
240 {
241   GstCaps *othercaps, *target;
242   GstStructure *structure;
243   const GstCaps *templ;
244
245   templ = gst_pad_get_pad_template_caps (monoscope->srcpad);
246
247   GST_DEBUG_OBJECT (monoscope, "performing negotiation");
248
249   /* see what the peer can do */
250   othercaps = gst_pad_peer_get_caps (monoscope->srcpad);
251   if (othercaps) {
252     target = gst_caps_intersect (othercaps, templ);
253     gst_caps_unref (othercaps);
254
255     if (gst_caps_is_empty (target))
256       goto no_format;
257
258     gst_caps_truncate (target);
259   } else {
260     target = gst_caps_ref ((GstCaps *) templ);
261   }
262
263   structure = gst_caps_get_structure (target, 0);
264   gst_structure_fixate_field_nearest_int (structure, "width", 320);
265   gst_structure_fixate_field_nearest_int (structure, "height", 240);
266   gst_structure_fixate_field_nearest_fraction (structure, "framerate", 25, 1);
267
268   gst_pad_set_caps (monoscope->srcpad, target);
269   gst_caps_unref (target);
270
271   return TRUE;
272
273 no_format:
274   {
275     gst_caps_unref (target);
276     return FALSE;
277   }
278 }
279
280 static GstFlowReturn
281 get_buffer (GstMonoscope * monoscope, GstBuffer ** outbuf)
282 {
283   GstFlowReturn ret;
284
285   if (GST_PAD_CAPS (monoscope->srcpad) == NULL) {
286     if (!gst_monoscope_src_negotiate (monoscope))
287       return GST_FLOW_NOT_NEGOTIATED;
288   }
289
290   GST_LOG_OBJECT (monoscope, "allocating output buffer of size %d with caps %"
291       GST_PTR_FORMAT, monoscope->outsize, GST_PAD_CAPS (monoscope->srcpad));
292
293   ret =
294       gst_pad_alloc_buffer_and_set_caps (monoscope->srcpad,
295       GST_BUFFER_OFFSET_NONE, monoscope->outsize,
296       GST_PAD_CAPS (monoscope->srcpad), outbuf);
297
298   if (ret != GST_FLOW_OK)
299     return ret;
300
301   if (*outbuf == NULL)
302     return GST_FLOW_ERROR;
303
304   return GST_FLOW_OK;
305 }
306
307 static GstFlowReturn
308 gst_monoscope_chain (GstPad * pad, GstBuffer * inbuf)
309 {
310   GstFlowReturn flow_ret = GST_FLOW_OK;
311   GstMonoscope *monoscope;
312
313   monoscope = GST_MONOSCOPE (GST_PAD_PARENT (pad));
314
315   /* don't try to combine samples from discont buffer */
316   if (GST_BUFFER_FLAG_IS_SET (inbuf, GST_BUFFER_FLAG_DISCONT)) {
317     gst_adapter_clear (monoscope->adapter);
318     monoscope->next_ts = GST_CLOCK_TIME_NONE;
319   }
320
321   /* Match timestamps from the incoming audio */
322   if (GST_BUFFER_TIMESTAMP (inbuf) != GST_CLOCK_TIME_NONE)
323     monoscope->next_ts = GST_BUFFER_TIMESTAMP (inbuf);
324
325   GST_LOG_OBJECT (monoscope, "in buffer has %d samples, ts=%" GST_TIME_FORMAT,
326       GST_BUFFER_SIZE (inbuf) / monoscope->bps,
327       GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (inbuf)));
328
329   gst_adapter_push (monoscope->adapter, inbuf);
330   inbuf = NULL;
331
332   /* Collect samples until we have enough for an output frame */
333   while (flow_ret == GST_FLOW_OK) {
334     gint16 *samples;
335     GstBuffer *outbuf = NULL;
336     guint32 *pixels, avail, bytesperframe;
337
338     avail = gst_adapter_available (monoscope->adapter);
339     GST_LOG_OBJECT (monoscope, "bytes avail now %u", avail);
340
341     /* do negotiation if not done yet, so ->spf etc. is set */
342     if (GST_PAD_CAPS (monoscope->srcpad) == NULL) {
343       flow_ret = get_buffer (monoscope, &outbuf);
344       if (flow_ret != GST_FLOW_OK)
345         goto out;
346       gst_buffer_unref (outbuf);
347       outbuf = NULL;
348     }
349
350     bytesperframe = monoscope->spf * monoscope->bps;
351     if (avail < bytesperframe)
352       break;
353
354     /* FIXME: something is wrong with QoS, we are skipping way too much
355      * stuff even with very low CPU loads */
356 #if 0
357     if (monoscope->next_ts != -1) {
358       gboolean need_skip;
359       gint64 qostime;
360
361       qostime = gst_segment_to_running_time (&monoscope->segment,
362           GST_FORMAT_TIME, monoscope->next_ts);
363
364       GST_OBJECT_LOCK (monoscope);
365       /* check for QoS, don't compute buffers that are known to be late */
366       need_skip =
367           GST_CLOCK_TIME_IS_VALID (monoscope->earliest_time) &&
368           qostime <= monoscope->earliest_time;
369       GST_OBJECT_UNLOCK (monoscope);
370
371       if (need_skip) {
372         GST_WARNING_OBJECT (monoscope,
373             "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
374             GST_TIME_ARGS (qostime), GST_TIME_ARGS (monoscope->earliest_time));
375         goto skip;
376       }
377     }
378 #endif
379
380     samples = (gint16 *) gst_adapter_peek (monoscope->adapter, bytesperframe);
381
382     if (monoscope->spf < 512) {
383       gint16 in_data[512], i;
384
385       for (i = 0; i < 512; ++i) {
386         gdouble off;
387
388         off = ((gdouble) i * (gdouble) monoscope->spf) / 512.0;
389         in_data[i] = samples[MIN ((guint) off, monoscope->spf)];
390       }
391       pixels = monoscope_update (monoscope->visstate, in_data);
392     } else {
393       /* not really correct, but looks much prettier */
394       pixels = monoscope_update (monoscope->visstate, samples);
395     }
396
397     flow_ret = get_buffer (monoscope, &outbuf);
398     if (flow_ret != GST_FLOW_OK)
399       goto out;
400
401     memcpy (GST_BUFFER_DATA (outbuf), pixels, monoscope->outsize);
402
403     GST_BUFFER_TIMESTAMP (outbuf) = monoscope->next_ts;
404     GST_BUFFER_DURATION (outbuf) = monoscope->frame_duration;
405
406     flow_ret = gst_pad_push (monoscope->srcpad, outbuf);
407
408 #if 0
409   skip:
410 #endif
411
412     if (GST_CLOCK_TIME_IS_VALID (monoscope->next_ts))
413       monoscope->next_ts += monoscope->frame_duration;
414
415     gst_adapter_flush (monoscope->adapter, bytesperframe);
416   }
417
418 out:
419
420   return flow_ret;
421 }
422
423 static gboolean
424 gst_monoscope_sink_event (GstPad * pad, GstEvent * event)
425 {
426   GstMonoscope *monoscope;
427   gboolean res;
428
429   monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
430
431   switch (GST_EVENT_TYPE (event)) {
432     case GST_EVENT_FLUSH_START:
433       res = gst_pad_push_event (monoscope->srcpad, event);
434       break;
435     case GST_EVENT_FLUSH_STOP:
436       gst_monoscope_reset (monoscope);
437       res = gst_pad_push_event (monoscope->srcpad, event);
438       break;
439     case GST_EVENT_NEWSEGMENT:
440     {
441       GstFormat format;
442       gdouble rate, arate;
443       gint64 start, stop, time;
444       gboolean update;
445
446       /* the newsegment values are used to clip the input samples
447        * and to convert the incomming timestamps to running time so
448        * we can do QoS */
449       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
450           &start, &stop, &time);
451
452       /* now configure the values */
453       gst_segment_set_newsegment_full (&monoscope->segment, update,
454           rate, arate, format, start, stop, time);
455
456       res = gst_pad_push_event (monoscope->srcpad, event);
457       break;
458     }
459     default:
460       res = gst_pad_push_event (monoscope->srcpad, event);
461       break;
462   }
463
464   gst_object_unref (monoscope);
465
466   return res;
467 }
468
469 static gboolean
470 gst_monoscope_src_event (GstPad * pad, GstEvent * event)
471 {
472   GstMonoscope *monoscope;
473   gboolean res;
474
475   monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
476
477   switch (GST_EVENT_TYPE (event)) {
478     case GST_EVENT_QOS:{
479       gdouble proportion;
480       GstClockTimeDiff diff;
481       GstClockTime timestamp;
482
483       gst_event_parse_qos (event, &proportion, &diff, &timestamp);
484
485       /* save stuff for the _chain() function */
486       GST_OBJECT_LOCK (monoscope);
487       monoscope->proportion = proportion;
488       if (diff >= 0)
489         /* we're late, this is a good estimate for next displayable
490          * frame (see part-qos.txt) */
491         monoscope->earliest_time =
492             timestamp + 2 * diff + monoscope->frame_duration;
493       else
494         monoscope->earliest_time = timestamp + diff;
495       GST_OBJECT_UNLOCK (monoscope);
496
497       res = gst_pad_push_event (monoscope->sinkpad, event);
498       break;
499     }
500     default:
501       res = gst_pad_push_event (monoscope->sinkpad, event);
502       break;
503   }
504   gst_object_unref (monoscope);
505
506   return res;
507 }
508
509 static GstStateChangeReturn
510 gst_monoscope_change_state (GstElement * element, GstStateChange transition)
511 {
512   GstMonoscope *monoscope = GST_MONOSCOPE (element);
513   GstStateChangeReturn ret;
514
515   switch (transition) {
516     case GST_STATE_CHANGE_NULL_TO_READY:
517       break;
518     case GST_STATE_CHANGE_READY_TO_PAUSED:
519       gst_monoscope_reset (monoscope);
520       break;
521     default:
522       break;
523   }
524
525   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
526
527   switch (transition) {
528     case GST_STATE_CHANGE_PAUSED_TO_READY:
529       break;
530     case GST_STATE_CHANGE_READY_TO_NULL:
531       break;
532     default:
533       break;
534   }
535
536   return ret;
537 }
538
539 static gboolean
540 plugin_init (GstPlugin * plugin)
541 {
542   GST_DEBUG_CATEGORY_INIT (monoscope_debug, "monoscope", 0,
543       "monoscope element");
544
545   return gst_element_register (plugin, "monoscope",
546       GST_RANK_NONE, GST_TYPE_MONOSCOPE);
547 }
548
549 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
550     GST_VERSION_MINOR,
551     "monoscope",
552     "Monoscope visualization",
553     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);