1 /* gstgoom.c: implementation of goom drawing element
2 * Copyright (C) <2001> Richard Boulton <richard@tartarus.org>
3 * (C) <2006> Wim Taymans <wim at fluendo dot com>
4 * (C) <2011> Wim Taymans <wim.taymans at gmail dot com>
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.
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.
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.
23 * SECTION:element-goom
24 * @see_also: synaesthesia
26 * Goom is an audio visualisation element. It creates warping structures
27 * based on the incoming audio signal.
30 * <title>Example launch line</title>
32 * gst-launch -v audiotestsrc ! goom ! videoconvert ! xvimagesink
44 #include <gst/video/video.h>
45 #include <gst/audio/audio.h>
52 GST_DEBUG_CATEGORY (goom_debug);
53 #define GST_CAT_DEFAULT goom_debug
55 #define DEFAULT_WIDTH 320
56 #define DEFAULT_HEIGHT 240
57 #define DEFAULT_FPS_N 25
58 #define DEFAULT_FPS_D 1
60 /* signals and args */
73 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
76 #if G_BYTE_ORDER == G_BIG_ENDIAN
77 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("xRGB"))
79 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("BGRx"))
83 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", /* the name of the pads */
84 GST_PAD_SINK, /* type of the pad */
85 GST_PAD_ALWAYS, /* ALWAYS/SOMETIMES */
86 GST_STATIC_CAPS ("audio/x-raw, "
87 "format = (string) " GST_AUDIO_NE (S16) ", "
88 "rate = (int) [ 8000, 96000 ], " "channels = (int) { 1, 2 }")
92 static void gst_goom_finalize (GObject * object);
94 static GstStateChangeReturn gst_goom_change_state (GstElement * element,
95 GstStateChange transition);
97 static GstFlowReturn gst_goom_chain (GstPad * pad, GstObject * parent,
99 static gboolean gst_goom_src_event (GstPad * pad, GstObject * parent,
101 static gboolean gst_goom_sink_event (GstPad * pad, GstObject * parent,
104 static gboolean gst_goom_src_query (GstPad * pad, GstObject * parent,
107 #define gst_goom_parent_class parent_class
108 G_DEFINE_TYPE (GstGoom, gst_goom, GST_TYPE_ELEMENT);
111 gst_goom_class_init (GstGoomClass * klass)
113 GObjectClass *gobject_class;
114 GstElementClass *gstelement_class;
116 gobject_class = (GObjectClass *) klass;
117 gstelement_class = (GstElementClass *) klass;
119 gobject_class->finalize = gst_goom_finalize;
121 gst_element_class_set_details_simple (gstelement_class, "GOOM: what a GOOM!",
123 "Takes frames of data and outputs video frames using the GOOM filter",
124 "Wim Taymans <wim@fluendo.com>");
125 gst_element_class_add_pad_template (gstelement_class,
126 gst_static_pad_template_get (&sink_template));
127 gst_element_class_add_pad_template (gstelement_class,
128 gst_static_pad_template_get (&src_template));
130 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_goom_change_state);
134 gst_goom_init (GstGoom * goom)
136 /* create the sink and src pads */
137 goom->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
138 gst_pad_set_chain_function (goom->sinkpad,
139 GST_DEBUG_FUNCPTR (gst_goom_chain));
140 gst_pad_set_event_function (goom->sinkpad,
141 GST_DEBUG_FUNCPTR (gst_goom_sink_event));
142 gst_element_add_pad (GST_ELEMENT (goom), goom->sinkpad);
144 goom->srcpad = gst_pad_new_from_static_template (&src_template, "src");
145 gst_pad_set_event_function (goom->srcpad,
146 GST_DEBUG_FUNCPTR (gst_goom_src_event));
147 gst_pad_set_query_function (goom->srcpad,
148 GST_DEBUG_FUNCPTR (gst_goom_src_query));
149 gst_element_add_pad (GST_ELEMENT (goom), goom->srcpad);
151 goom->adapter = gst_adapter_new ();
153 goom->width = DEFAULT_WIDTH;
154 goom->height = DEFAULT_HEIGHT;
155 goom->fps_n = DEFAULT_FPS_N; /* desired frame rate */
156 goom->fps_d = DEFAULT_FPS_D; /* desired frame rate */
161 goom->plugin = goom_init (goom->width, goom->height);
165 gst_goom_finalize (GObject * object)
167 GstGoom *goom = GST_GOOM (object);
169 goom_close (goom->plugin);
172 g_object_unref (goom->adapter);
174 gst_object_unref (goom->pool);
176 G_OBJECT_CLASS (parent_class)->finalize (object);
180 gst_goom_reset (GstGoom * goom)
182 gst_adapter_clear (goom->adapter);
183 gst_segment_init (&goom->segment, GST_FORMAT_UNDEFINED);
185 GST_OBJECT_LOCK (goom);
186 goom->proportion = 1.0;
187 goom->earliest_time = -1;
188 GST_OBJECT_UNLOCK (goom);
192 gst_goom_sink_setcaps (GstGoom * goom, GstCaps * caps)
194 GstStructure *structure;
196 structure = gst_caps_get_structure (caps, 0);
198 gst_structure_get_int (structure, "channels", &goom->channels);
199 gst_structure_get_int (structure, "rate", &goom->rate);
201 goom->bps = goom->channels * sizeof (gint16);
207 gst_goom_src_setcaps (GstGoom * goom, GstCaps * caps)
209 GstStructure *structure;
212 structure = gst_caps_get_structure (caps, 0);
213 if (!gst_structure_get_int (structure, "width", &goom->width) ||
214 !gst_structure_get_int (structure, "height", &goom->height) ||
215 !gst_structure_get_fraction (structure, "framerate", &goom->fps_n,
219 goom_set_resolution (goom->plugin, goom->width, goom->height);
221 /* size of the output buffer in bytes, depth is always 4 bytes */
222 goom->outsize = goom->width * goom->height * 4;
224 gst_util_uint64_scale_int (GST_SECOND, goom->fps_d, goom->fps_n);
225 goom->spf = gst_util_uint64_scale_int (goom->rate, goom->fps_d, goom->fps_n);
226 goom->bpf = goom->spf * goom->bps;
228 GST_DEBUG_OBJECT (goom, "dimension %dx%d, framerate %d/%d, spf %d",
229 goom->width, goom->height, goom->fps_n, goom->fps_d, goom->spf);
231 res = gst_pad_push_event (goom->srcpad, gst_event_new_caps (caps));
238 GST_DEBUG_OBJECT (goom, "error parsing caps");
244 gst_goom_src_negotiate (GstGoom * goom)
246 GstCaps *othercaps, *target;
247 GstStructure *structure;
250 GstBufferPool *pool = NULL;
251 guint size, min, max, prefix, alignment;
253 templ = gst_pad_get_pad_template_caps (goom->srcpad);
255 GST_DEBUG_OBJECT (goom, "performing negotiation");
257 /* see what the peer can do */
258 othercaps = gst_pad_peer_query_caps (goom->srcpad, NULL);
260 target = gst_caps_intersect (othercaps, templ);
261 gst_caps_unref (othercaps);
262 gst_caps_unref (templ);
264 if (gst_caps_is_empty (target))
267 gst_caps_truncate (target);
272 structure = gst_caps_get_structure (target, 0);
273 gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
274 gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
275 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
276 DEFAULT_FPS_N, DEFAULT_FPS_D);
278 gst_goom_src_setcaps (goom, target);
280 /* try to get a bufferpool now */
281 /* find a pool for the negotiated caps now */
282 query = gst_query_new_allocation (target, TRUE);
284 if (gst_pad_peer_query (goom->srcpad, query)) {
285 /* we got configuration from our peer, parse them */
286 gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
289 size = goom->outsize;
296 GstStructure *config;
298 /* we did not get a pool, make one ourselves then */
299 pool = gst_buffer_pool_new ();
301 config = gst_buffer_pool_get_config (pool);
302 gst_buffer_pool_config_set (config, target, size, min, max, prefix,
304 gst_buffer_pool_set_config (pool, config);
308 gst_object_unref (goom->pool);
312 gst_buffer_pool_set_active (pool, TRUE);
314 gst_caps_unref (target);
320 gst_caps_unref (target);
326 gst_goom_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
331 goom = GST_GOOM (parent);
333 switch (GST_EVENT_TYPE (event)) {
337 GstClockTimeDiff diff;
338 GstClockTime timestamp;
340 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
342 /* save stuff for the _chain() function */
343 GST_OBJECT_LOCK (goom);
344 goom->proportion = proportion;
346 /* we're late, this is a good estimate for next displayable
347 * frame (see part-qos.txt) */
348 goom->earliest_time = timestamp + 2 * diff + goom->duration;
350 goom->earliest_time = timestamp + diff;
351 GST_OBJECT_UNLOCK (goom);
353 res = gst_pad_push_event (goom->sinkpad, event);
357 res = gst_pad_push_event (goom->sinkpad, event);
365 gst_goom_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
370 goom = GST_GOOM (parent);
372 switch (GST_EVENT_TYPE (event)) {
377 gst_event_parse_caps (event, &caps);
378 res = gst_goom_sink_setcaps (goom, caps);
381 case GST_EVENT_FLUSH_START:
382 res = gst_pad_push_event (goom->srcpad, event);
384 case GST_EVENT_FLUSH_STOP:
385 gst_goom_reset (goom);
386 res = gst_pad_push_event (goom->srcpad, event);
388 case GST_EVENT_SEGMENT:
390 /* the newsegment values are used to clip the input samples
391 * and to convert the incomming timestamps to running time so
393 gst_event_copy_segment (event, &goom->segment);
395 res = gst_pad_push_event (goom->srcpad, event);
399 res = gst_pad_push_event (goom->srcpad, event);
407 gst_goom_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
409 gboolean res = FALSE;
412 goom = GST_GOOM (parent);
414 switch (GST_QUERY_TYPE (query)) {
415 case GST_QUERY_LATENCY:
417 /* We need to send the query upstream and add the returned latency to our
419 GstClockTime min_latency, max_latency;
421 GstClockTime our_latency;
427 if ((res = gst_pad_peer_query (goom->sinkpad, query))) {
428 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
430 GST_DEBUG_OBJECT (goom, "Peer latency: min %"
431 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
432 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
434 /* the max samples we must buffer buffer */
435 max_samples = MAX (GOOM_SAMPLES, goom->spf);
437 gst_util_uint64_scale_int (max_samples, GST_SECOND, goom->rate);
439 GST_DEBUG_OBJECT (goom, "Our latency: %" GST_TIME_FORMAT,
440 GST_TIME_ARGS (our_latency));
442 /* we add some latency but only if we need to buffer more than what
443 * upstream gives us */
444 min_latency += our_latency;
445 if (max_latency != -1)
446 max_latency += our_latency;
448 GST_DEBUG_OBJECT (goom, "Calculated total latency : min %"
449 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
450 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
452 gst_query_set_latency (query, TRUE, min_latency, max_latency);
457 res = gst_pad_query_default (pad, parent, query);
464 /* make sure we are negotiated */
466 ensure_negotiated (GstGoom * goom)
468 gboolean reconfigure;
470 reconfigure = gst_pad_check_reconfigure (goom->srcpad);
472 /* we don't know an output format yet, pick one */
473 if (reconfigure || !gst_pad_has_current_caps (goom->srcpad)) {
474 if (!gst_goom_src_negotiate (goom))
475 return GST_FLOW_NOT_NEGOTIATED;
482 gst_goom_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
486 GstBuffer *outbuf = NULL;
488 goom = GST_GOOM (parent);
489 if (goom->bps == 0) {
490 ret = GST_FLOW_NOT_NEGOTIATED;
494 /* Make sure have an output format */
495 ret = ensure_negotiated (goom);
496 if (ret != GST_FLOW_OK) {
497 gst_buffer_unref (buffer);
501 /* don't try to combine samples from discont buffer */
502 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
503 gst_adapter_clear (goom->adapter);
506 GST_DEBUG_OBJECT (goom,
507 "Input buffer has %" G_GSIZE_FORMAT " samples, time=%" G_GUINT64_FORMAT,
508 gst_buffer_get_size (buffer) / goom->bps, GST_BUFFER_TIMESTAMP (buffer));
510 /* Collect samples until we have enough for an output frame */
511 gst_adapter_push (goom->adapter, buffer);
519 guint avail, to_flush;
520 guint64 dist, timestamp;
522 avail = gst_adapter_available (goom->adapter);
523 GST_DEBUG_OBJECT (goom, "avail now %u", avail);
525 /* we need GOOM_SAMPLES to get a meaningful result from goom. */
526 if (avail < (GOOM_SAMPLES * goom->bps))
529 /* we also need enough samples to produce one frame at least */
530 if (avail < goom->bpf)
533 GST_DEBUG_OBJECT (goom, "processing buffer");
535 /* get timestamp of the current adapter byte */
536 timestamp = gst_adapter_prev_timestamp (goom->adapter, &dist);
537 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
538 /* convert bytes to time */
540 timestamp += gst_util_uint64_scale_int (dist, GST_SECOND, goom->rate);
543 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
547 qostime = gst_segment_to_running_time (&goom->segment, GST_FORMAT_TIME,
548 timestamp) + goom->duration;
550 GST_OBJECT_LOCK (goom);
551 /* check for QoS, don't compute buffers that are known to be late */
552 need_skip = goom->earliest_time != -1 && qostime <= goom->earliest_time;
553 GST_OBJECT_UNLOCK (goom);
556 GST_WARNING_OBJECT (goom,
557 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
558 GST_TIME_ARGS (qostime), GST_TIME_ARGS (goom->earliest_time));
563 /* get next GOOM_SAMPLES, we have at least this amount of samples */
565 (const guint16 *) gst_adapter_map (goom->adapter,
566 GOOM_SAMPLES * goom->bps);
568 if (goom->channels == 2) {
569 for (i = 0; i < GOOM_SAMPLES; i++) {
570 goom->datain[0][i] = *data++;
571 goom->datain[1][i] = *data++;
574 for (i = 0; i < GOOM_SAMPLES; i++) {
575 goom->datain[0][i] = *data;
576 goom->datain[1][i] = *data++;
580 /* alloc a buffer if we don't have one yet, this happens
581 * when we pushed a buffer in this while loop before */
582 if (outbuf == NULL) {
583 GST_DEBUG_OBJECT (goom, "allocating output buffer");
584 ret = gst_buffer_pool_acquire_buffer (goom->pool, &outbuf, NULL);
585 if (ret != GST_FLOW_OK) {
586 gst_adapter_unmap (goom->adapter);
591 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
592 GST_BUFFER_DURATION (outbuf) = goom->duration;
594 out_frame = (guchar *) goom_update (goom->plugin, goom->datain, 0, 0);
595 gst_buffer_fill (outbuf, 0, out_frame, goom->outsize);
597 gst_adapter_unmap (goom->adapter);
599 GST_DEBUG ("Pushing frame with time=%" GST_TIME_FORMAT ", duration=%"
600 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
601 GST_TIME_ARGS (goom->duration));
603 ret = gst_pad_push (goom->srcpad, outbuf);
607 /* Now flush the samples we needed for this frame, which might be more than
608 * the samples we used (GOOM_SAMPLES). */
609 to_flush = goom->bpf;
611 GST_DEBUG_OBJECT (goom, "finished frame, flushing %u bytes from input",
613 gst_adapter_flush (goom->adapter, to_flush);
615 if (ret != GST_FLOW_OK)
620 gst_buffer_unref (outbuf);
627 static GstStateChangeReturn
628 gst_goom_change_state (GstElement * element, GstStateChange transition)
630 GstGoom *goom = GST_GOOM (element);
631 GstStateChangeReturn ret;
633 switch (transition) {
634 case GST_STATE_CHANGE_NULL_TO_READY:
636 case GST_STATE_CHANGE_READY_TO_PAUSED:
637 gst_goom_reset (goom);
643 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
645 switch (transition) {
646 case GST_STATE_CHANGE_PAUSED_TO_READY:
648 gst_buffer_pool_set_active (goom->pool, FALSE);
649 gst_object_replace ((GstObject **) & goom->pool, NULL);
652 case GST_STATE_CHANGE_READY_TO_NULL:
662 plugin_init (GstPlugin * plugin)
664 GST_DEBUG_CATEGORY_INIT (goom_debug, "goom", 0, "goom visualisation element");
670 return gst_element_register (plugin, "goom", GST_RANK_NONE, GST_TYPE_GOOM);
673 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
676 "GOOM visualization filter",
677 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)