gst/monoscope/gstmonoscope.c: Don't unref buffers of which we've already given away...
[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         goto out;
350       gst_buffer_unref (outbuf);
351       outbuf = NULL;
352     }
353
354     bytesperframe = monoscope->spf * monoscope->bps;
355     if (avail < bytesperframe)
356       break;
357
358     /* FIXME: something is wrong with QoS, we are skipping way too much
359      * stuff even with very low CPU loads */
360 #if 0
361     if (monoscope->next_ts != -1) {
362       gboolean need_skip;
363       gint64 qostime;
364
365       qostime = gst_segment_to_running_time (&monoscope->segment,
366           GST_FORMAT_TIME, monoscope->next_ts);
367
368       GST_OBJECT_LOCK (monoscope);
369       /* check for QoS, don't compute buffers that are known to be late */
370       need_skip =
371           GST_CLOCK_TIME_IS_VALID (monoscope->earliest_time) &&
372           qostime <= monoscope->earliest_time;
373       GST_OBJECT_UNLOCK (monoscope);
374
375       if (need_skip) {
376         GST_WARNING_OBJECT (monoscope,
377             "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
378             GST_TIME_ARGS (qostime), GST_TIME_ARGS (monoscope->earliest_time));
379         goto skip;
380       }
381     }
382 #endif
383
384     samples = (gint16 *) gst_adapter_peek (monoscope->adapter, bytesperframe);
385
386     if (monoscope->spf < 512) {
387       gint16 in_data[512], i;
388
389       for (i = 0; i < 512; ++i) {
390         gdouble off;
391
392         off = ((gdouble) i * (gdouble) monoscope->spf) / 512.0;
393         in_data[i] = samples[MIN ((guint) off, monoscope->spf)];
394       }
395       pixels = monoscope_update (monoscope->visstate, in_data);
396     } else {
397       /* not really correct, but looks much prettier */
398       pixels = monoscope_update (monoscope->visstate, samples);
399     }
400
401     flow_ret = get_buffer (monoscope, &outbuf);
402     if (flow_ret != GST_FLOW_OK)
403       goto out;
404
405     memcpy (GST_BUFFER_DATA (outbuf), pixels, monoscope->outsize);
406
407     GST_BUFFER_TIMESTAMP (outbuf) = monoscope->next_ts;
408     GST_BUFFER_DURATION (outbuf) = monoscope->frame_duration;
409
410     flow_ret = gst_pad_push (monoscope->srcpad, outbuf);
411
412 #if 0
413   skip:
414 #endif
415
416     if (GST_CLOCK_TIME_IS_VALID (monoscope->next_ts))
417       monoscope->next_ts += monoscope->frame_duration;
418
419     gst_adapter_flush (monoscope->adapter, bytesperframe);
420   }
421
422 out:
423
424   return flow_ret;
425 }
426
427 static gboolean
428 gst_monoscope_sink_event (GstPad * pad, GstEvent * event)
429 {
430   GstMonoscope *monoscope;
431   gboolean res;
432
433   monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
434
435   switch (GST_EVENT_TYPE (event)) {
436     case GST_EVENT_FLUSH_START:
437       res = gst_pad_push_event (monoscope->srcpad, event);
438       break;
439     case GST_EVENT_FLUSH_STOP:
440       gst_monoscope_reset (monoscope);
441       res = gst_pad_push_event (monoscope->srcpad, event);
442       break;
443     case GST_EVENT_NEWSEGMENT:
444     {
445       GstFormat format;
446       gdouble rate, arate;
447       gint64 start, stop, time;
448       gboolean update;
449
450       /* the newsegment values are used to clip the input samples
451        * and to convert the incomming timestamps to running time so
452        * we can do QoS */
453       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
454           &start, &stop, &time);
455
456       /* now configure the values */
457       gst_segment_set_newsegment_full (&monoscope->segment, update,
458           rate, arate, format, start, stop, time);
459
460       res = gst_pad_push_event (monoscope->srcpad, event);
461       break;
462     }
463     default:
464       res = gst_pad_push_event (monoscope->srcpad, event);
465       break;
466   }
467
468   gst_object_unref (monoscope);
469
470   return res;
471 }
472
473 static gboolean
474 gst_monoscope_src_event (GstPad * pad, GstEvent * event)
475 {
476   GstMonoscope *monoscope;
477   gboolean res;
478
479   monoscope = GST_MONOSCOPE (gst_pad_get_parent (pad));
480
481   switch (GST_EVENT_TYPE (event)) {
482     case GST_EVENT_QOS:{
483       gdouble proportion;
484       GstClockTimeDiff diff;
485       GstClockTime timestamp;
486
487       gst_event_parse_qos (event, &proportion, &diff, &timestamp);
488
489       /* save stuff for the _chain() function */
490       GST_OBJECT_LOCK (monoscope);
491       monoscope->proportion = proportion;
492       if (diff >= 0)
493         /* we're late, this is a good estimate for next displayable
494          * frame (see part-qos.txt) */
495         monoscope->earliest_time =
496             timestamp + 2 * diff + monoscope->frame_duration;
497       else
498         monoscope->earliest_time = timestamp + diff;
499       GST_OBJECT_UNLOCK (monoscope);
500
501       res = gst_pad_push_event (monoscope->sinkpad, event);
502       break;
503     }
504     default:
505       res = gst_pad_push_event (monoscope->sinkpad, event);
506       break;
507   }
508   gst_object_unref (monoscope);
509
510   return res;
511 }
512
513 static GstStateChangeReturn
514 gst_monoscope_change_state (GstElement * element, GstStateChange transition)
515 {
516   GstMonoscope *monoscope = GST_MONOSCOPE (element);
517   GstStateChangeReturn ret;
518
519
520   switch (transition) {
521     case GST_STATE_CHANGE_NULL_TO_READY:
522       break;
523     case GST_STATE_CHANGE_READY_TO_PAUSED:
524       gst_monoscope_reset (monoscope);
525       break;
526     default:
527       break;
528   }
529
530   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
531
532   switch (transition) {
533     case GST_STATE_CHANGE_PAUSED_TO_READY:
534       break;
535     case GST_STATE_CHANGE_READY_TO_NULL:
536       break;
537     default:
538       break;
539   }
540
541   return ret;
542 }
543
544 static gboolean
545 plugin_init (GstPlugin * plugin)
546 {
547   GST_DEBUG_CATEGORY_INIT (monoscope_debug, "monoscope", 0,
548       "monoscope element");
549
550   return gst_element_register (plugin, "monoscope",
551       GST_RANK_NONE, GST_TYPE_MONOSCOPE);
552 }
553
554 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
555     GST_VERSION_MINOR,
556     "monoscope",
557     "Monoscope visualization",
558     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);