228df9f289aeda30f3c79ad76236968999b49592
[platform/upstream/gstreamer.git] / gst / goom2k1 / gstgoom.c
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  *
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.
9  *
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.
14  *
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.
19  */
20
21 /**
22  * SECTION:element-goom2k1
23  * @see_also: goom, synaesthesia
24  *
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.
28  *
29  * <refsect2>
30  * <title>Example launch line</title>
31  * |[
32  * gst-launch -v audiotestsrc ! goom2k1 ! ffmpegcolorspace ! xvimagesink
33  * ]|
34  * </refsect2>
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <string.h>
42 #include <gst/gst.h>
43 #include "gstgoom.h"
44 #include <gst/video/video.h>
45 #include <gst/audio/audio.h>
46 #include "goom_core.h"
47
48 GST_DEBUG_CATEGORY_STATIC (goom_debug);
49 #define GST_CAT_DEFAULT goom_debug
50
51 #define DEFAULT_WIDTH  320
52 #define DEFAULT_HEIGHT 240
53 #define DEFAULT_FPS_N  25
54 #define DEFAULT_FPS_D  1
55
56 /* signals and args */
57 enum
58 {
59   /* FILL ME */
60   LAST_SIGNAL
61 };
62
63 enum
64 {
65   ARG_0
66       /* FILL ME */
67 };
68
69 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
70     GST_PAD_SRC,
71     GST_PAD_ALWAYS,
72 #if G_BYTE_ORDER == G_BIG_ENDIAN
73     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("xRGB"))
74 #else
75     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("BGRx"))
76 #endif
77     );
78
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; "
87         "audio/x-raw, "
88         "format = (string) " GST_AUDIO_NE (S16) ", "
89         "rate = (int) [ 8000, 96000 ], "
90         "channels = (int) 2, "
91         "channel-mask = (bitmask) 0x3, " "layout = (string) interleaved")
92     );
93
94 static void gst_goom_finalize (GObject * object);
95
96 static GstStateChangeReturn gst_goom_change_state (GstElement * element,
97     GstStateChange transition);
98
99 static GstFlowReturn gst_goom_chain (GstPad * pad, GstObject * parent,
100     GstBuffer * buffer);
101 static gboolean gst_goom_src_event (GstPad * pad, GstObject * parent,
102     GstEvent * event);
103 static gboolean gst_goom_sink_event (GstPad * pad, GstObject * parent,
104     GstEvent * event);
105
106 static gboolean gst_goom_src_query (GstPad * pad, GstObject * parent,
107     GstQuery * query);
108
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);
113
114 static void
115 gst_goom_class_init (GstGoomClass * klass)
116 {
117   GObjectClass *gobject_class;
118   GstElementClass *gstelement_class;
119
120   gobject_class = (GObjectClass *) klass;
121   gstelement_class = (GstElementClass *) klass;
122
123   parent_class = g_type_class_peek_parent (klass);
124
125   gobject_class->finalize = gst_goom_finalize;
126
127   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_goom_change_state);
128
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));
137
138   GST_DEBUG_CATEGORY_INIT (goom_debug, "goom", 0, "goom visualisation element");
139 }
140
141 static void
142 gst_goom_init (GstGoom * goom)
143 {
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);
151
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);
158
159   goom->adapter = gst_adapter_new ();
160
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 */
165   goom->channels = 0;
166   goom->rate = 0;
167   goom->duration = 0;
168
169   goom_init (&(goom->goomdata), goom->width, goom->height);
170 }
171
172 static void
173 gst_goom_finalize (GObject * object)
174 {
175   GstGoom *goom = GST_GOOM (object);
176
177   goom_close (&(goom->goomdata));
178
179   g_object_unref (goom->adapter);
180
181   G_OBJECT_CLASS (parent_class)->finalize (object);
182 }
183
184 static void
185 gst_goom_reset (GstGoom * goom)
186 {
187   gst_adapter_clear (goom->adapter);
188   gst_segment_init (&goom->segment, GST_FORMAT_UNDEFINED);
189
190   GST_OBJECT_LOCK (goom);
191   goom->proportion = 1.0;
192   goom->earliest_time = -1;
193   GST_OBJECT_UNLOCK (goom);
194 }
195
196 static gboolean
197 gst_goom_sink_setcaps (GstGoom * goom, GstCaps * caps)
198 {
199   GstStructure *structure;
200   gboolean res;
201
202   structure = gst_caps_get_structure (caps, 0);
203
204   res = gst_structure_get_int (structure, "channels", &goom->channels);
205   res &= gst_structure_get_int (structure, "rate", &goom->rate);
206
207   goom->bps = goom->channels * sizeof (gint16);
208
209   return res;
210 }
211
212 static gboolean
213 gst_goom_src_setcaps (GstGoom * goom, GstCaps * caps)
214 {
215   GstStructure *structure;
216
217   structure = gst_caps_get_structure (caps, 0);
218
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,
222           &goom->fps_d))
223     goto error;
224
225   goom_set_resolution (&(goom->goomdata), goom->width, goom->height);
226
227   /* size of the output buffer in bytes, depth is always 4 bytes */
228   goom->outsize = goom->width * goom->height * 4;
229   goom->duration =
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;
233
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);
236
237   return gst_pad_push_event (goom->srcpad, gst_event_new_caps (caps));
238
239   /* ERRORS */
240 error:
241   {
242     GST_DEBUG_OBJECT (goom, "error parsing caps");
243     return FALSE;
244   }
245 }
246
247 static gboolean
248 gst_goom_src_negotiate (GstGoom * goom)
249 {
250   GstCaps *othercaps, *target;
251   GstStructure *structure;
252   GstCaps *templ;
253   GstQuery *query;
254   GstBufferPool *pool = NULL;
255   guint size, min, max, prefix, padding, alignment;
256
257   templ = gst_pad_get_pad_template_caps (goom->srcpad);
258
259   GST_DEBUG_OBJECT (goom, "performing negotiation");
260
261   /* see what the peer can do */
262   othercaps = gst_pad_peer_query_caps (goom->srcpad, NULL);
263   if (othercaps) {
264     target = gst_caps_intersect (othercaps, templ);
265     gst_caps_unref (templ);
266     gst_caps_unref (othercaps);
267
268     if (gst_caps_is_empty (target))
269       goto no_format;
270
271     target = gst_caps_truncate (target);
272   } else {
273     target = gst_caps_copy (templ);
274   }
275
276   structure = gst_caps_get_structure (target, 0);
277   gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
278   gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
279   gst_structure_fixate_field_nearest_fraction (structure, "framerate",
280       DEFAULT_FPS_N, DEFAULT_FPS_D);
281
282   gst_goom_src_setcaps (goom, target);
283
284   /* try to get a bufferpool now */
285   /* find a pool for the negotiated caps now */
286   query = gst_query_new_allocation (target, TRUE);
287
288   if (gst_pad_peer_query (goom->srcpad, query)) {
289     /* we got configuration from our peer, parse them */
290     gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
291         &padding, &alignment, &pool);
292   } else {
293     size = goom->outsize;
294     min = max = 0;
295     prefix = 0;
296     padding = 0;
297     alignment = 0;
298   }
299
300   if (pool == NULL) {
301     GstStructure *config;
302
303     /* we did not get a pool, make one ourselves then */
304     pool = gst_buffer_pool_new ();
305
306     config = gst_buffer_pool_get_config (pool);
307     gst_buffer_pool_config_set (config, target, size, min, max, prefix,
308         padding, alignment);
309     gst_buffer_pool_set_config (pool, config);
310   }
311
312   if (goom->pool)
313     gst_object_unref (goom->pool);
314   goom->pool = pool;
315
316   /* and activate */
317   gst_buffer_pool_set_active (pool, TRUE);
318
319   gst_caps_unref (target);
320
321   return TRUE;
322
323 no_format:
324   {
325     gst_caps_unref (target);
326     return FALSE;
327   }
328 }
329
330 static gboolean
331 gst_goom_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
332 {
333   gboolean res;
334   GstGoom *goom;
335
336   goom = GST_GOOM (parent);
337
338   switch (GST_EVENT_TYPE (event)) {
339     case GST_EVENT_QOS:
340     {
341       gdouble proportion;
342       GstClockTimeDiff diff;
343       GstClockTime timestamp;
344
345       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
346
347       /* save stuff for the _chain() function */
348       GST_OBJECT_LOCK (goom);
349       goom->proportion = proportion;
350       if (diff >= 0)
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;
354       else
355         goom->earliest_time = timestamp + diff;
356       GST_OBJECT_UNLOCK (goom);
357
358       res = gst_pad_push_event (goom->sinkpad, event);
359       break;
360     }
361     default:
362       res = gst_pad_push_event (goom->sinkpad, event);
363       break;
364   }
365
366   return res;
367 }
368
369 static gboolean
370 gst_goom_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
371 {
372   gboolean res;
373   GstGoom *goom;
374
375   goom = GST_GOOM (parent);
376
377   switch (GST_EVENT_TYPE (event)) {
378     case GST_EVENT_CAPS:
379     {
380       GstCaps *caps;
381
382       gst_event_parse_caps (event, &caps);
383       res = gst_goom_sink_setcaps (goom, caps);
384       gst_event_unref (event);
385       break;
386     }
387     case GST_EVENT_FLUSH_START:
388       res = gst_pad_push_event (goom->srcpad, event);
389       break;
390     case GST_EVENT_FLUSH_STOP:
391       gst_goom_reset (goom);
392       res = gst_pad_push_event (goom->srcpad, event);
393       break;
394     case GST_EVENT_SEGMENT:
395     {
396       /* the newsegment values are used to clip the input samples
397        * and to convert the incomming timestamps to running time so
398        * we can do QoS */
399       gst_event_copy_segment (event, &goom->segment);
400
401       res = gst_pad_push_event (goom->srcpad, event);
402       break;
403     }
404     default:
405       res = gst_pad_push_event (goom->srcpad, event);
406       break;
407   }
408
409   return res;
410 }
411
412 static gboolean
413 gst_goom_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
414 {
415   gboolean res;
416   GstGoom *goom;
417
418   goom = GST_GOOM (parent);
419
420   switch (GST_QUERY_TYPE (query)) {
421     case GST_QUERY_LATENCY:
422     {
423       /* We need to send the query upstream and add the returned latency to our
424        * own */
425       GstClockTime min_latency, max_latency;
426       gboolean us_live;
427       GstClockTime our_latency;
428       guint max_samples;
429
430       if ((res = gst_pad_peer_query (goom->sinkpad, query))) {
431         gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
432
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));
436
437         /* the max samples we must buffer buffer */
438         max_samples = MAX (GOOM_SAMPLES, goom->spf);
439         our_latency =
440             gst_util_uint64_scale_int (max_samples, GST_SECOND, goom->rate);
441
442         GST_DEBUG_OBJECT (goom, "Our latency: %" GST_TIME_FORMAT,
443             GST_TIME_ARGS (our_latency));
444
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;
450
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));
454
455         gst_query_set_latency (query, TRUE, min_latency, max_latency);
456       }
457       break;
458     }
459     default:
460       res = gst_pad_query_default (pad, parent, query);
461       break;
462   }
463
464   return res;
465 }
466
467 /* make sure we are negotiated */
468 static GstFlowReturn
469 ensure_negotiated (GstGoom * goom)
470 {
471   gboolean reconfigure;
472
473   reconfigure = gst_pad_check_reconfigure (goom->srcpad);
474
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;
479   }
480   return GST_FLOW_OK;
481 }
482
483 static GstFlowReturn
484 gst_goom_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
485 {
486   GstGoom *goom;
487   GstFlowReturn ret;
488   GstBuffer *outbuf = NULL;
489
490   goom = GST_GOOM (parent);
491
492   /* If we don't have an output format yet, preallocate a buffer to try and
493    * set one */
494   if (goom->bps == 0) {
495     gst_buffer_unref (buffer);
496     ret = GST_FLOW_NOT_NEGOTIATED;
497     goto beach;
498   }
499
500   /* Make sure have an output format */
501   ret = ensure_negotiated (goom);
502   if (ret != GST_FLOW_OK) {
503     gst_buffer_unref (buffer);
504     goto beach;
505   }
506
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);
510   }
511
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));
515
516   /* Collect samples until we have enough for an output frame */
517   gst_adapter_push (goom->adapter, buffer);
518
519   ret = GST_FLOW_OK;
520
521   while (TRUE) {
522     const guint16 *data;
523     gboolean need_skip;
524     guchar *out_frame;
525     gint i;
526     guint avail, to_flush;
527     guint64 dist, timestamp;
528
529     avail = gst_adapter_available (goom->adapter);
530     GST_DEBUG_OBJECT (goom, "avail now %u", avail);
531
532     /* we need GOOM_SAMPLES to get a meaningful result from goom. */
533     if (avail < (GOOM_SAMPLES * goom->bps))
534       break;
535
536     /* we also need enough samples to produce one frame at least */
537     if (avail < goom->bpf)
538       break;
539
540     GST_DEBUG_OBJECT (goom, "processing buffer");
541
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 */
546       dist /= goom->bps;
547       timestamp += gst_util_uint64_scale_int (dist, GST_SECOND, goom->rate);
548     }
549
550     if (timestamp != -1) {
551       gint64 qostime;
552
553       qostime = gst_segment_to_running_time (&goom->segment, GST_FORMAT_TIME,
554           timestamp);
555       qostime += goom->duration;
556
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);
561
562       if (need_skip) {
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));
566         goto skip;
567       }
568     }
569
570     /* get next GOOM_SAMPLES, we have at least this amount of samples */
571     data =
572         (const guint16 *) gst_adapter_map (goom->adapter,
573         GOOM_SAMPLES * goom->bps);
574
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++;
579       }
580     } else {
581       for (i = 0; i < GOOM_SAMPLES; i++) {
582         goom->datain[0][i] = *data;
583         goom->datain[1][i] = *data++;
584       }
585     }
586
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);
594         goto beach;
595       }
596     }
597
598     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
599     GST_BUFFER_DURATION (outbuf) = goom->duration;
600
601     out_frame = (guchar *) goom_update (&(goom->goomdata), goom->datain);
602     gst_buffer_fill (outbuf, 0, out_frame, goom->outsize);
603
604     gst_adapter_unmap (goom->adapter);
605
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));
609
610     ret = gst_pad_push (goom->srcpad, outbuf);
611     outbuf = NULL;
612
613   skip:
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;
617
618     GST_DEBUG_OBJECT (goom, "finished frame, flushing %u bytes from input",
619         to_flush);
620     gst_adapter_flush (goom->adapter, to_flush);
621
622     if (ret != GST_FLOW_OK)
623       break;
624   }
625
626   if (outbuf != NULL)
627     gst_buffer_unref (outbuf);
628
629 beach:
630
631   return ret;
632 }
633
634 static GstStateChangeReturn
635 gst_goom_change_state (GstElement * element, GstStateChange transition)
636 {
637   GstGoom *goom = GST_GOOM (element);
638   GstStateChangeReturn ret;
639
640   switch (transition) {
641     case GST_STATE_CHANGE_NULL_TO_READY:
642       break;
643     case GST_STATE_CHANGE_READY_TO_PAUSED:
644       gst_goom_reset (goom);
645       break;
646     default:
647       break;
648   }
649
650   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
651
652   switch (transition) {
653     case GST_STATE_CHANGE_PAUSED_TO_READY:
654       if (goom->pool) {
655         gst_buffer_pool_set_active (goom->pool, FALSE);
656         gst_object_replace ((GstObject **) & goom->pool, NULL);
657       }
658       break;
659     case GST_STATE_CHANGE_READY_TO_NULL:
660       break;
661     default:
662       break;
663   }
664
665   return ret;
666 }
667
668 static gboolean
669 plugin_init (GstPlugin * plugin)
670 {
671   return gst_element_register (plugin, "goom2k1", GST_RANK_NONE, GST_TYPE_GOOM);
672 }
673
674 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
675     GST_VERSION_MINOR,
676     "goom2k1",
677     "GOOM 2k1 visualization filter",
678     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)