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