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 ], "
89 "channels = (int) 1, "
90 "layout = (string) interleaved; "
92 "format = (string) " GST_AUDIO_NE (S16) ", "
93 "rate = (int) [ 8000, 96000 ], "
94 "channels = (int) 2, "
95 "channel-mask = (bitmask) 0x3, " "layout = (string) interleaved")
99 static void gst_goom_finalize (GObject * object);
101 static GstStateChangeReturn gst_goom_change_state (GstElement * element,
102 GstStateChange transition);
104 static GstFlowReturn gst_goom_chain (GstPad * pad, GstObject * parent,
106 static gboolean gst_goom_src_event (GstPad * pad, GstObject * parent,
108 static gboolean gst_goom_sink_event (GstPad * pad, GstObject * parent,
111 static gboolean gst_goom_src_query (GstPad * pad, GstObject * parent,
114 #define gst_goom_parent_class parent_class
115 G_DEFINE_TYPE (GstGoom, gst_goom, GST_TYPE_ELEMENT);
118 gst_goom_class_init (GstGoomClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
126 gobject_class->finalize = gst_goom_finalize;
128 gst_element_class_set_static_metadata (gstelement_class, "GOOM: what a GOOM!",
130 "Takes frames of data and outputs video frames using the GOOM filter",
131 "Wim Taymans <wim@fluendo.com>");
132 gst_element_class_add_pad_template (gstelement_class,
133 gst_static_pad_template_get (&sink_template));
134 gst_element_class_add_pad_template (gstelement_class,
135 gst_static_pad_template_get (&src_template));
137 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_goom_change_state);
141 gst_goom_init (GstGoom * goom)
143 /* create the sink and src pads */
144 goom->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
145 gst_pad_set_chain_function (goom->sinkpad,
146 GST_DEBUG_FUNCPTR (gst_goom_chain));
147 gst_pad_set_event_function (goom->sinkpad,
148 GST_DEBUG_FUNCPTR (gst_goom_sink_event));
149 gst_element_add_pad (GST_ELEMENT (goom), goom->sinkpad);
151 goom->srcpad = gst_pad_new_from_static_template (&src_template, "src");
152 gst_pad_set_event_function (goom->srcpad,
153 GST_DEBUG_FUNCPTR (gst_goom_src_event));
154 gst_pad_set_query_function (goom->srcpad,
155 GST_DEBUG_FUNCPTR (gst_goom_src_query));
156 gst_element_add_pad (GST_ELEMENT (goom), goom->srcpad);
158 goom->adapter = gst_adapter_new ();
160 goom->width = DEFAULT_WIDTH;
161 goom->height = DEFAULT_HEIGHT;
162 goom->fps_n = DEFAULT_FPS_N; /* desired frame rate */
163 goom->fps_d = DEFAULT_FPS_D; /* desired frame rate */
168 goom->plugin = goom_init (goom->width, goom->height);
172 gst_goom_finalize (GObject * object)
174 GstGoom *goom = GST_GOOM (object);
176 goom_close (goom->plugin);
179 g_object_unref (goom->adapter);
181 gst_object_unref (goom->pool);
183 G_OBJECT_CLASS (parent_class)->finalize (object);
187 gst_goom_reset (GstGoom * goom)
189 gst_adapter_clear (goom->adapter);
190 gst_segment_init (&goom->segment, GST_FORMAT_UNDEFINED);
192 GST_OBJECT_LOCK (goom);
193 goom->proportion = 1.0;
194 goom->earliest_time = -1;
195 GST_OBJECT_UNLOCK (goom);
199 gst_goom_sink_setcaps (GstGoom * goom, GstCaps * caps)
201 GstStructure *structure;
203 structure = gst_caps_get_structure (caps, 0);
205 gst_structure_get_int (structure, "channels", &goom->channels);
206 gst_structure_get_int (structure, "rate", &goom->rate);
208 goom->bps = goom->channels * sizeof (gint16);
214 gst_goom_src_setcaps (GstGoom * goom, GstCaps * caps)
216 GstStructure *structure;
219 structure = gst_caps_get_structure (caps, 0);
220 if (!gst_structure_get_int (structure, "width", &goom->width) ||
221 !gst_structure_get_int (structure, "height", &goom->height) ||
222 !gst_structure_get_fraction (structure, "framerate", &goom->fps_n,
226 goom_set_resolution (goom->plugin, goom->width, goom->height);
228 /* size of the output buffer in bytes, depth is always 4 bytes */
229 goom->outsize = goom->width * goom->height * 4;
231 gst_util_uint64_scale_int (GST_SECOND, goom->fps_d, goom->fps_n);
232 goom->spf = gst_util_uint64_scale_int (goom->rate, goom->fps_d, goom->fps_n);
233 goom->bpf = goom->spf * goom->bps;
235 GST_DEBUG_OBJECT (goom, "dimension %dx%d, framerate %d/%d, spf %d",
236 goom->width, goom->height, goom->fps_n, goom->fps_d, goom->spf);
238 res = gst_pad_push_event (goom->srcpad, gst_event_new_caps (caps));
245 GST_DEBUG_OBJECT (goom, "error parsing caps");
251 gst_goom_src_negotiate (GstGoom * goom)
253 GstCaps *othercaps, *target;
254 GstStructure *structure;
258 GstStructure *config;
259 guint size, min, max;
261 templ = gst_pad_get_pad_template_caps (goom->srcpad);
263 GST_DEBUG_OBJECT (goom, "performing negotiation");
265 /* see what the peer can do */
266 othercaps = gst_pad_peer_query_caps (goom->srcpad, NULL);
268 target = gst_caps_intersect (othercaps, templ);
269 gst_caps_unref (othercaps);
270 gst_caps_unref (templ);
272 if (gst_caps_is_empty (target))
275 target = gst_caps_truncate (target);
280 structure = gst_caps_get_structure (target, 0);
281 gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
282 gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
283 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
284 DEFAULT_FPS_N, DEFAULT_FPS_D);
286 gst_goom_src_setcaps (goom, target);
288 /* try to get a bufferpool now */
289 /* find a pool for the negotiated caps now */
290 query = gst_query_new_allocation (target, TRUE);
292 if (!gst_pad_peer_query (goom->srcpad, query)) {
293 /* no problem, we use the query defaults */
294 GST_DEBUG_OBJECT (goom, "ALLOCATION query failed");
297 if (gst_query_get_n_allocation_pools (query) > 0) {
298 /* we got configuration from our peer, parse them */
299 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
302 size = goom->outsize;
307 /* we did not get a pool, make one ourselves then */
308 pool = gst_buffer_pool_new ();
311 config = gst_buffer_pool_get_config (pool);
312 gst_buffer_pool_config_set_params (config, target, size, min, max);
313 gst_buffer_pool_set_config (pool, config);
316 gst_buffer_pool_set_active (goom->pool, FALSE);
317 gst_object_unref (goom->pool);
322 gst_buffer_pool_set_active (pool, TRUE);
324 gst_caps_unref (target);
330 gst_caps_unref (target);
336 gst_goom_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
341 goom = GST_GOOM (parent);
343 switch (GST_EVENT_TYPE (event)) {
347 GstClockTimeDiff diff;
348 GstClockTime timestamp;
350 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
352 /* save stuff for the _chain() function */
353 GST_OBJECT_LOCK (goom);
354 goom->proportion = proportion;
356 /* we're late, this is a good estimate for next displayable
357 * frame (see part-qos.txt) */
358 goom->earliest_time = timestamp + 2 * diff + goom->duration;
360 goom->earliest_time = timestamp + diff;
361 GST_OBJECT_UNLOCK (goom);
363 res = gst_pad_push_event (goom->sinkpad, event);
367 res = gst_pad_push_event (goom->sinkpad, event);
375 gst_goom_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
380 goom = GST_GOOM (parent);
382 switch (GST_EVENT_TYPE (event)) {
387 gst_event_parse_caps (event, &caps);
388 res = gst_goom_sink_setcaps (goom, caps);
389 gst_event_unref (event);
392 case GST_EVENT_FLUSH_START:
393 res = gst_pad_push_event (goom->srcpad, event);
395 case GST_EVENT_FLUSH_STOP:
396 gst_goom_reset (goom);
397 res = gst_pad_push_event (goom->srcpad, event);
399 case GST_EVENT_SEGMENT:
401 /* the newsegment values are used to clip the input samples
402 * and to convert the incomming timestamps to running time so
404 gst_event_copy_segment (event, &goom->segment);
406 res = gst_pad_push_event (goom->srcpad, event);
410 res = gst_pad_push_event (goom->srcpad, event);
418 gst_goom_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
420 gboolean res = FALSE;
423 goom = GST_GOOM (parent);
425 switch (GST_QUERY_TYPE (query)) {
426 case GST_QUERY_LATENCY:
428 /* We need to send the query upstream and add the returned latency to our
430 GstClockTime min_latency, max_latency;
432 GstClockTime our_latency;
438 if ((res = gst_pad_peer_query (goom->sinkpad, query))) {
439 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
441 GST_DEBUG_OBJECT (goom, "Peer latency: min %"
442 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
443 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
445 /* the max samples we must buffer buffer */
446 max_samples = MAX (GOOM_SAMPLES, goom->spf);
448 gst_util_uint64_scale_int (max_samples, GST_SECOND, goom->rate);
450 GST_DEBUG_OBJECT (goom, "Our latency: %" GST_TIME_FORMAT,
451 GST_TIME_ARGS (our_latency));
453 /* we add some latency but only if we need to buffer more than what
454 * upstream gives us */
455 min_latency += our_latency;
456 if (max_latency != -1)
457 max_latency += our_latency;
459 GST_DEBUG_OBJECT (goom, "Calculated total latency : min %"
460 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
461 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
463 gst_query_set_latency (query, TRUE, min_latency, max_latency);
468 res = gst_pad_query_default (pad, parent, query);
475 /* make sure we are negotiated */
477 ensure_negotiated (GstGoom * goom)
479 gboolean reconfigure;
481 reconfigure = gst_pad_check_reconfigure (goom->srcpad);
483 /* we don't know an output format yet, pick one */
484 if (reconfigure || !gst_pad_has_current_caps (goom->srcpad)) {
485 if (!gst_goom_src_negotiate (goom))
486 return GST_FLOW_NOT_NEGOTIATED;
493 gst_goom_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
497 GstBuffer *outbuf = NULL;
499 goom = GST_GOOM (parent);
500 if (goom->bps == 0) {
501 gst_buffer_unref (buffer);
502 ret = GST_FLOW_NOT_NEGOTIATED;
506 /* Make sure have an output format */
507 ret = ensure_negotiated (goom);
508 if (ret != GST_FLOW_OK) {
509 gst_buffer_unref (buffer);
513 /* don't try to combine samples from discont buffer */
514 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
515 gst_adapter_clear (goom->adapter);
518 GST_DEBUG_OBJECT (goom,
519 "Input buffer has %" G_GSIZE_FORMAT " samples, time=%" G_GUINT64_FORMAT,
520 gst_buffer_get_size (buffer) / goom->bps, GST_BUFFER_TIMESTAMP (buffer));
522 /* Collect samples until we have enough for an output frame */
523 gst_adapter_push (goom->adapter, buffer);
531 guint avail, to_flush;
532 guint64 dist, timestamp;
534 avail = gst_adapter_available (goom->adapter);
535 GST_DEBUG_OBJECT (goom, "avail now %u", avail);
537 /* we need GOOM_SAMPLES to get a meaningful result from goom. */
538 if (avail < (GOOM_SAMPLES * goom->bps))
541 /* we also need enough samples to produce one frame at least */
542 if (avail < goom->bpf)
545 GST_DEBUG_OBJECT (goom, "processing buffer");
547 /* get timestamp of the current adapter byte */
548 timestamp = gst_adapter_prev_timestamp (goom->adapter, &dist);
549 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
550 /* convert bytes to time */
552 timestamp += gst_util_uint64_scale_int (dist, GST_SECOND, goom->rate);
555 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
559 qostime = gst_segment_to_running_time (&goom->segment, GST_FORMAT_TIME,
560 timestamp) + goom->duration;
562 GST_OBJECT_LOCK (goom);
563 /* check for QoS, don't compute buffers that are known to be late */
564 need_skip = goom->earliest_time != -1 && qostime <= goom->earliest_time;
565 GST_OBJECT_UNLOCK (goom);
568 GST_WARNING_OBJECT (goom,
569 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
570 GST_TIME_ARGS (qostime), GST_TIME_ARGS (goom->earliest_time));
575 /* get next GOOM_SAMPLES, we have at least this amount of samples */
577 (const guint16 *) gst_adapter_map (goom->adapter,
578 GOOM_SAMPLES * goom->bps);
580 if (goom->channels == 2) {
581 for (i = 0; i < GOOM_SAMPLES; i++) {
582 goom->datain[0][i] = *data++;
583 goom->datain[1][i] = *data++;
586 for (i = 0; i < GOOM_SAMPLES; i++) {
587 goom->datain[0][i] = *data;
588 goom->datain[1][i] = *data++;
592 /* alloc a buffer if we don't have one yet, this happens
593 * when we pushed a buffer in this while loop before */
594 if (outbuf == NULL) {
595 GST_DEBUG_OBJECT (goom, "allocating output buffer");
596 ret = gst_buffer_pool_acquire_buffer (goom->pool, &outbuf, NULL);
597 if (ret != GST_FLOW_OK) {
598 gst_adapter_unmap (goom->adapter);
603 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
604 GST_BUFFER_DURATION (outbuf) = goom->duration;
606 out_frame = (guchar *) goom_update (goom->plugin, goom->datain, 0, 0);
607 gst_buffer_fill (outbuf, 0, out_frame, goom->outsize);
609 gst_adapter_unmap (goom->adapter);
611 GST_DEBUG ("Pushing frame with time=%" GST_TIME_FORMAT ", duration=%"
612 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
613 GST_TIME_ARGS (goom->duration));
615 ret = gst_pad_push (goom->srcpad, outbuf);
619 /* Now flush the samples we needed for this frame, which might be more than
620 * the samples we used (GOOM_SAMPLES). */
621 to_flush = goom->bpf;
623 GST_DEBUG_OBJECT (goom, "finished frame, flushing %u bytes from input",
625 gst_adapter_flush (goom->adapter, to_flush);
627 if (ret != GST_FLOW_OK)
632 gst_buffer_unref (outbuf);
639 static GstStateChangeReturn
640 gst_goom_change_state (GstElement * element, GstStateChange transition)
642 GstGoom *goom = GST_GOOM (element);
643 GstStateChangeReturn ret;
645 switch (transition) {
646 case GST_STATE_CHANGE_NULL_TO_READY:
648 case GST_STATE_CHANGE_READY_TO_PAUSED:
649 gst_goom_reset (goom);
655 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
657 switch (transition) {
658 case GST_STATE_CHANGE_PAUSED_TO_READY:
660 gst_buffer_pool_set_active (goom->pool, FALSE);
661 gst_object_replace ((GstObject **) & goom->pool, NULL);
664 case GST_STATE_CHANGE_READY_TO_NULL:
674 plugin_init (GstPlugin * plugin)
676 GST_DEBUG_CATEGORY_INIT (goom_debug, "goom", 0, "goom visualisation element");
682 return gst_element_register (plugin, "goom", GST_RANK_NONE, GST_TYPE_GOOM);
685 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
688 "GOOM visualization filter",
689 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)