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