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>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * SECTION:element-goom2k1
23 * @see_also: goom, synaesthesia
25 * Goom2k1 is an audio visualisation element. It creates warping structures
26 * based on the incomming audio signal. Goom2k1 is the older version of the
27 * visualisation. Also available is goom2k4, with a different look.
30 * <title>Example launch line</title>
32 * gst-launch -v audiotestsrc ! goom2k1 ! ffmpegcolorspace ! xvimagesink
44 #include <gst/video/video.h>
45 #include <gst/audio/audio.h>
46 #include "goom_core.h"
48 GST_DEBUG_CATEGORY_STATIC (goom_debug);
49 #define GST_CAT_DEFAULT goom_debug
51 #define DEFAULT_WIDTH 320
52 #define DEFAULT_HEIGHT 240
53 #define DEFAULT_FPS_N 25
54 #define DEFAULT_FPS_D 1
56 /* signals and args */
69 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
72 #if G_BYTE_ORDER == G_BIG_ENDIAN
73 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("xRGB"))
75 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("BGRx"))
79 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", /* the name of the pads */
80 GST_PAD_SINK, /* type of the pad */
81 GST_PAD_ALWAYS, /* ALWAYS/SOMETIMES */
82 GST_STATIC_CAPS ("audio/x-raw, "
83 "format = (string) " GST_AUDIO_NE (S16) ", "
84 "rate = (int) [ 8000, 96000 ], "
85 "channels = (int) 1, "
86 "layout = (string) interleaved; "
88 "format = (string) " GST_AUDIO_NE (S16) ", "
89 "rate = (int) [ 8000, 96000 ], "
90 "channels = (int) 2, "
91 "channel-mask = (bitmask) 0x3, " "layout = (string) interleaved")
94 static void gst_goom_finalize (GObject * object);
96 static GstStateChangeReturn gst_goom_change_state (GstElement * element,
97 GstStateChange transition);
99 static GstFlowReturn gst_goom_chain (GstPad * pad, GstObject * parent,
101 static gboolean gst_goom_src_event (GstPad * pad, GstObject * parent,
103 static gboolean gst_goom_sink_event (GstPad * pad, GstObject * parent,
106 static gboolean gst_goom_src_query (GstPad * pad, GstObject * parent,
109 #define gst_goom_parent_class parent_class
110 typedef GstGoom GstGoom2k1;
111 typedef GstGoomClass GstGoom2k1Class;
112 G_DEFINE_TYPE (GstGoom2k1, gst_goom, GST_TYPE_ELEMENT);
115 gst_goom_class_init (GstGoomClass * klass)
117 GObjectClass *gobject_class;
118 GstElementClass *gstelement_class;
120 gobject_class = (GObjectClass *) klass;
121 gstelement_class = (GstElementClass *) klass;
123 parent_class = g_type_class_peek_parent (klass);
125 gobject_class->finalize = gst_goom_finalize;
127 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_goom_change_state);
129 gst_element_class_set_details_simple (gstelement_class,
130 "GOOM: what a GOOM! 2k1 edition", "Visualization",
131 "Takes frames of data and outputs video frames using the GOOM 2k1 filter",
132 "Wim Taymans <wim@fluendo.com>");
133 gst_element_class_add_pad_template (gstelement_class,
134 gst_static_pad_template_get (&sink_template));
135 gst_element_class_add_pad_template (gstelement_class,
136 gst_static_pad_template_get (&src_template));
138 GST_DEBUG_CATEGORY_INIT (goom_debug, "goom", 0, "goom visualisation element");
142 gst_goom_init (GstGoom * goom)
144 /* create the sink and src pads */
145 goom->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
146 gst_pad_set_chain_function (goom->sinkpad,
147 GST_DEBUG_FUNCPTR (gst_goom_chain));
148 gst_pad_set_event_function (goom->sinkpad,
149 GST_DEBUG_FUNCPTR (gst_goom_sink_event));
150 gst_element_add_pad (GST_ELEMENT (goom), goom->sinkpad);
152 goom->srcpad = gst_pad_new_from_static_template (&src_template, "src");
153 gst_pad_set_event_function (goom->srcpad,
154 GST_DEBUG_FUNCPTR (gst_goom_src_event));
155 gst_pad_set_query_function (goom->srcpad,
156 GST_DEBUG_FUNCPTR (gst_goom_src_query));
157 gst_element_add_pad (GST_ELEMENT (goom), goom->srcpad);
159 goom->adapter = gst_adapter_new ();
161 goom->width = DEFAULT_WIDTH;
162 goom->height = DEFAULT_HEIGHT;
163 goom->fps_n = DEFAULT_FPS_N; /* desired frame rate */
164 goom->fps_d = DEFAULT_FPS_D; /* desired frame rate */
169 goom_init (&(goom->goomdata), goom->width, goom->height);
173 gst_goom_finalize (GObject * object)
175 GstGoom *goom = GST_GOOM (object);
177 goom_close (&(goom->goomdata));
179 g_object_unref (goom->adapter);
181 G_OBJECT_CLASS (parent_class)->finalize (object);
185 gst_goom_reset (GstGoom * goom)
187 gst_adapter_clear (goom->adapter);
188 gst_segment_init (&goom->segment, GST_FORMAT_UNDEFINED);
190 GST_OBJECT_LOCK (goom);
191 goom->proportion = 1.0;
192 goom->earliest_time = -1;
193 GST_OBJECT_UNLOCK (goom);
197 gst_goom_sink_setcaps (GstGoom * goom, GstCaps * caps)
199 GstStructure *structure;
202 structure = gst_caps_get_structure (caps, 0);
204 res = gst_structure_get_int (structure, "channels", &goom->channels);
205 res &= gst_structure_get_int (structure, "rate", &goom->rate);
207 goom->bps = goom->channels * sizeof (gint16);
213 gst_goom_src_setcaps (GstGoom * goom, GstCaps * caps)
215 GstStructure *structure;
217 structure = gst_caps_get_structure (caps, 0);
219 if (!gst_structure_get_int (structure, "width", &goom->width) ||
220 !gst_structure_get_int (structure, "height", &goom->height) ||
221 !gst_structure_get_fraction (structure, "framerate", &goom->fps_n,
225 goom_set_resolution (&(goom->goomdata), goom->width, goom->height);
227 /* size of the output buffer in bytes, depth is always 4 bytes */
228 goom->outsize = goom->width * goom->height * 4;
230 gst_util_uint64_scale_int (GST_SECOND, goom->fps_d, goom->fps_n);
231 goom->spf = gst_util_uint64_scale_int (goom->rate, goom->fps_d, goom->fps_n);
232 goom->bpf = goom->spf * goom->bps;
234 GST_DEBUG_OBJECT (goom, "dimension %dx%d, framerate %d/%d, spf %d",
235 goom->width, goom->height, goom->fps_n, goom->fps_d, goom->spf);
237 return gst_pad_push_event (goom->srcpad, gst_event_new_caps (caps));
242 GST_DEBUG_OBJECT (goom, "error parsing caps");
248 gst_goom_src_negotiate (GstGoom * goom)
250 GstCaps *othercaps, *target;
251 GstStructure *structure;
254 GstBufferPool *pool = NULL;
255 guint size, min, max, prefix, alignment;
257 templ = gst_pad_get_pad_template_caps (goom->srcpad);
259 GST_DEBUG_OBJECT (goom, "performing negotiation");
261 /* see what the peer can do */
262 othercaps = gst_pad_peer_query_caps (goom->srcpad, NULL);
264 target = gst_caps_intersect (othercaps, templ);
265 gst_caps_unref (templ);
266 gst_caps_unref (othercaps);
268 if (gst_caps_is_empty (target))
271 target = gst_caps_make_writable (target);
272 gst_caps_truncate (target);
274 target = gst_caps_copy (templ);
277 structure = gst_caps_get_structure (target, 0);
278 gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
279 gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
280 gst_structure_fixate_field_nearest_fraction (structure, "framerate",
281 DEFAULT_FPS_N, DEFAULT_FPS_D);
283 gst_goom_src_setcaps (goom, target);
285 /* try to get a bufferpool now */
286 /* find a pool for the negotiated caps now */
287 query = gst_query_new_allocation (target, TRUE);
289 if (gst_pad_peer_query (goom->srcpad, query)) {
290 /* we got configuration from our peer, parse them */
291 gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
294 size = goom->outsize;
301 GstStructure *config;
303 /* we did not get a pool, make one ourselves then */
304 pool = gst_buffer_pool_new ();
306 config = gst_buffer_pool_get_config (pool);
307 gst_buffer_pool_config_set (config, target, size, min, max, prefix,
309 gst_buffer_pool_set_config (pool, config);
313 gst_object_unref (goom->pool);
317 gst_buffer_pool_set_active (pool, TRUE);
319 gst_caps_unref (target);
325 gst_caps_unref (target);
331 gst_goom_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
336 goom = GST_GOOM (parent);
338 switch (GST_EVENT_TYPE (event)) {
342 GstClockTimeDiff diff;
343 GstClockTime timestamp;
345 gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
347 /* save stuff for the _chain() function */
348 GST_OBJECT_LOCK (goom);
349 goom->proportion = proportion;
351 /* we're late, this is a good estimate for next displayable
352 * frame (see part-qos.txt) */
353 goom->earliest_time = timestamp + 2 * diff + goom->duration;
355 goom->earliest_time = timestamp + diff;
356 GST_OBJECT_UNLOCK (goom);
358 res = gst_pad_push_event (goom->sinkpad, event);
362 res = gst_pad_push_event (goom->sinkpad, event);
370 gst_goom_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
375 goom = GST_GOOM (parent);
377 switch (GST_EVENT_TYPE (event)) {
382 gst_event_parse_caps (event, &caps);
383 res = gst_goom_sink_setcaps (goom, caps);
384 gst_event_unref (event);
387 case GST_EVENT_FLUSH_START:
388 res = gst_pad_push_event (goom->srcpad, event);
390 case GST_EVENT_FLUSH_STOP:
391 gst_goom_reset (goom);
392 res = gst_pad_push_event (goom->srcpad, event);
394 case GST_EVENT_SEGMENT:
396 /* the newsegment values are used to clip the input samples
397 * and to convert the incomming timestamps to running time so
399 gst_event_copy_segment (event, &goom->segment);
401 res = gst_pad_push_event (goom->srcpad, event);
405 res = gst_pad_push_event (goom->srcpad, event);
413 gst_goom_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
418 goom = GST_GOOM (parent);
420 switch (GST_QUERY_TYPE (query)) {
421 case GST_QUERY_LATENCY:
423 /* We need to send the query upstream and add the returned latency to our
425 GstClockTime min_latency, max_latency;
427 GstClockTime our_latency;
430 if ((res = gst_pad_peer_query (goom->sinkpad, query))) {
431 gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
433 GST_DEBUG_OBJECT (goom, "Peer latency: min %"
434 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
435 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
437 /* the max samples we must buffer buffer */
438 max_samples = MAX (GOOM_SAMPLES, goom->spf);
440 gst_util_uint64_scale_int (max_samples, GST_SECOND, goom->rate);
442 GST_DEBUG_OBJECT (goom, "Our latency: %" GST_TIME_FORMAT,
443 GST_TIME_ARGS (our_latency));
445 /* we add some latency but only if we need to buffer more than what
446 * upstream gives us */
447 min_latency += our_latency;
448 if (max_latency != -1)
449 max_latency += our_latency;
451 GST_DEBUG_OBJECT (goom, "Calculated total latency : min %"
452 GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
453 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
455 gst_query_set_latency (query, TRUE, min_latency, max_latency);
460 res = gst_pad_query_default (pad, parent, query);
467 /* make sure we are negotiated */
469 ensure_negotiated (GstGoom * goom)
471 gboolean reconfigure;
473 reconfigure = gst_pad_check_reconfigure (goom->srcpad);
475 /* we don't know an output format yet, pick one */
476 if (reconfigure || !gst_pad_has_current_caps (goom->srcpad)) {
477 if (!gst_goom_src_negotiate (goom))
478 return GST_FLOW_NOT_NEGOTIATED;
484 gst_goom_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
488 GstBuffer *outbuf = NULL;
490 goom = GST_GOOM (parent);
492 /* If we don't have an output format yet, preallocate a buffer to try and
494 if (goom->bps == 0) {
495 gst_buffer_unref (buffer);
496 ret = GST_FLOW_NOT_NEGOTIATED;
500 /* Make sure have an output format */
501 ret = ensure_negotiated (goom);
502 if (ret != GST_FLOW_OK) {
503 gst_buffer_unref (buffer);
507 /* don't try to combine samples from discont buffer */
508 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
509 gst_adapter_clear (goom->adapter);
512 GST_DEBUG_OBJECT (goom,
513 "Input buffer has %" G_GSIZE_FORMAT " samples, time=%" G_GUINT64_FORMAT,
514 gst_buffer_get_size (buffer) / goom->bps, GST_BUFFER_TIMESTAMP (buffer));
516 /* Collect samples until we have enough for an output frame */
517 gst_adapter_push (goom->adapter, buffer);
526 guint avail, to_flush;
527 guint64 dist, timestamp;
529 avail = gst_adapter_available (goom->adapter);
530 GST_DEBUG_OBJECT (goom, "avail now %u", avail);
532 /* we need GOOM_SAMPLES to get a meaningful result from goom. */
533 if (avail < (GOOM_SAMPLES * goom->bps))
536 /* we also need enough samples to produce one frame at least */
537 if (avail < goom->bpf)
540 GST_DEBUG_OBJECT (goom, "processing buffer");
542 /* get timestamp of the current adapter byte */
543 timestamp = gst_adapter_prev_timestamp (goom->adapter, &dist);
544 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
545 /* convert bytes to time */
547 timestamp += gst_util_uint64_scale_int (dist, GST_SECOND, goom->rate);
550 if (timestamp != -1) {
553 qostime = gst_segment_to_running_time (&goom->segment, GST_FORMAT_TIME,
555 qostime += goom->duration;
557 GST_OBJECT_LOCK (goom);
558 /* check for QoS, don't compute buffers that are known to be late */
559 need_skip = goom->earliest_time != -1 && qostime <= goom->earliest_time;
560 GST_OBJECT_UNLOCK (goom);
563 GST_WARNING_OBJECT (goom,
564 "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
565 GST_TIME_ARGS (qostime), GST_TIME_ARGS (goom->earliest_time));
570 /* get next GOOM_SAMPLES, we have at least this amount of samples */
572 (const guint16 *) gst_adapter_map (goom->adapter,
573 GOOM_SAMPLES * goom->bps);
575 if (goom->channels == 2) {
576 for (i = 0; i < GOOM_SAMPLES; i++) {
577 goom->datain[0][i] = *data++;
578 goom->datain[1][i] = *data++;
581 for (i = 0; i < GOOM_SAMPLES; i++) {
582 goom->datain[0][i] = *data;
583 goom->datain[1][i] = *data++;
587 /* alloc a buffer if we don't have one yet, this happens
588 * when we pushed a buffer in this while loop before */
589 if (outbuf == NULL) {
590 GST_DEBUG_OBJECT (goom, "allocating output buffer");
591 ret = gst_buffer_pool_acquire_buffer (goom->pool, &outbuf, NULL);
592 if (ret != GST_FLOW_OK) {
593 gst_adapter_unmap (goom->adapter);
598 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
599 GST_BUFFER_DURATION (outbuf) = goom->duration;
601 out_frame = (guchar *) goom_update (&(goom->goomdata), goom->datain);
602 gst_buffer_fill (outbuf, 0, out_frame, goom->outsize);
604 gst_adapter_unmap (goom->adapter);
606 GST_DEBUG ("Pushing frame with time=%" GST_TIME_FORMAT ", duration=%"
607 GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
608 GST_TIME_ARGS (goom->duration));
610 ret = gst_pad_push (goom->srcpad, outbuf);
614 /* Now flush the samples we needed for this frame, which might be more than
615 * the samples we used (GOOM_SAMPLES). */
616 to_flush = goom->bpf;
618 GST_DEBUG_OBJECT (goom, "finished frame, flushing %u bytes from input",
620 gst_adapter_flush (goom->adapter, to_flush);
622 if (ret != GST_FLOW_OK)
627 gst_buffer_unref (outbuf);
634 static GstStateChangeReturn
635 gst_goom_change_state (GstElement * element, GstStateChange transition)
637 GstGoom *goom = GST_GOOM (element);
638 GstStateChangeReturn ret;
640 switch (transition) {
641 case GST_STATE_CHANGE_NULL_TO_READY:
643 case GST_STATE_CHANGE_READY_TO_PAUSED:
644 gst_goom_reset (goom);
650 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
652 switch (transition) {
653 case GST_STATE_CHANGE_PAUSED_TO_READY:
655 gst_buffer_pool_set_active (goom->pool, FALSE);
656 gst_object_replace ((GstObject **) & goom->pool, NULL);
659 case GST_STATE_CHANGE_READY_TO_NULL:
669 plugin_init (GstPlugin * plugin)
671 return gst_element_register (plugin, "goom2k1", GST_RANK_NONE, GST_TYPE_GOOM);
674 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
677 "GOOM 2k1 visualization filter",
678 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)