fix for caps api changes
[platform/upstream/gst-plugins-good.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, 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         &alignment, &pool);
292   } else {
293     size = goom->outsize;
294     min = max = 0;
295     prefix = 0;
296     alignment = 0;
297   }
298
299   if (pool == NULL) {
300     GstStructure *config;
301
302     /* we did not get a pool, make one ourselves then */
303     pool = gst_buffer_pool_new ();
304
305     config = gst_buffer_pool_get_config (pool);
306     gst_buffer_pool_config_set (config, target, size, min, max, prefix,
307         alignment);
308     gst_buffer_pool_set_config (pool, config);
309   }
310
311   if (goom->pool)
312     gst_object_unref (goom->pool);
313   goom->pool = pool;
314
315   /* and activate */
316   gst_buffer_pool_set_active (pool, TRUE);
317
318   gst_caps_unref (target);
319
320   return TRUE;
321
322 no_format:
323   {
324     gst_caps_unref (target);
325     return FALSE;
326   }
327 }
328
329 static gboolean
330 gst_goom_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
331 {
332   gboolean res;
333   GstGoom *goom;
334
335   goom = GST_GOOM (parent);
336
337   switch (GST_EVENT_TYPE (event)) {
338     case GST_EVENT_QOS:
339     {
340       gdouble proportion;
341       GstClockTimeDiff diff;
342       GstClockTime timestamp;
343
344       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
345
346       /* save stuff for the _chain() function */
347       GST_OBJECT_LOCK (goom);
348       goom->proportion = proportion;
349       if (diff >= 0)
350         /* we're late, this is a good estimate for next displayable
351          * frame (see part-qos.txt) */
352         goom->earliest_time = timestamp + 2 * diff + goom->duration;
353       else
354         goom->earliest_time = timestamp + diff;
355       GST_OBJECT_UNLOCK (goom);
356
357       res = gst_pad_push_event (goom->sinkpad, event);
358       break;
359     }
360     default:
361       res = gst_pad_push_event (goom->sinkpad, event);
362       break;
363   }
364
365   return res;
366 }
367
368 static gboolean
369 gst_goom_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
370 {
371   gboolean res;
372   GstGoom *goom;
373
374   goom = GST_GOOM (parent);
375
376   switch (GST_EVENT_TYPE (event)) {
377     case GST_EVENT_CAPS:
378     {
379       GstCaps *caps;
380
381       gst_event_parse_caps (event, &caps);
382       res = gst_goom_sink_setcaps (goom, caps);
383       gst_event_unref (event);
384       break;
385     }
386     case GST_EVENT_FLUSH_START:
387       res = gst_pad_push_event (goom->srcpad, event);
388       break;
389     case GST_EVENT_FLUSH_STOP:
390       gst_goom_reset (goom);
391       res = gst_pad_push_event (goom->srcpad, event);
392       break;
393     case GST_EVENT_SEGMENT:
394     {
395       /* the newsegment values are used to clip the input samples
396        * and to convert the incomming timestamps to running time so
397        * we can do QoS */
398       gst_event_copy_segment (event, &goom->segment);
399
400       res = gst_pad_push_event (goom->srcpad, event);
401       break;
402     }
403     default:
404       res = gst_pad_push_event (goom->srcpad, event);
405       break;
406   }
407
408   return res;
409 }
410
411 static gboolean
412 gst_goom_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
413 {
414   gboolean res;
415   GstGoom *goom;
416
417   goom = GST_GOOM (parent);
418
419   switch (GST_QUERY_TYPE (query)) {
420     case GST_QUERY_LATENCY:
421     {
422       /* We need to send the query upstream and add the returned latency to our
423        * own */
424       GstClockTime min_latency, max_latency;
425       gboolean us_live;
426       GstClockTime our_latency;
427       guint max_samples;
428
429       if ((res = gst_pad_peer_query (goom->sinkpad, query))) {
430         gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
431
432         GST_DEBUG_OBJECT (goom, "Peer latency: min %"
433             GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
434             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
435
436         /* the max samples we must buffer buffer */
437         max_samples = MAX (GOOM_SAMPLES, goom->spf);
438         our_latency =
439             gst_util_uint64_scale_int (max_samples, GST_SECOND, goom->rate);
440
441         GST_DEBUG_OBJECT (goom, "Our latency: %" GST_TIME_FORMAT,
442             GST_TIME_ARGS (our_latency));
443
444         /* we add some latency but only if we need to buffer more than what
445          * upstream gives us */
446         min_latency += our_latency;
447         if (max_latency != -1)
448           max_latency += our_latency;
449
450         GST_DEBUG_OBJECT (goom, "Calculated total latency : min %"
451             GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
452             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
453
454         gst_query_set_latency (query, TRUE, min_latency, max_latency);
455       }
456       break;
457     }
458     default:
459       res = gst_pad_query_default (pad, parent, query);
460       break;
461   }
462
463   return res;
464 }
465
466 /* make sure we are negotiated */
467 static GstFlowReturn
468 ensure_negotiated (GstGoom * goom)
469 {
470   gboolean reconfigure;
471
472   reconfigure = gst_pad_check_reconfigure (goom->srcpad);
473
474   /* we don't know an output format yet, pick one */
475   if (reconfigure || !gst_pad_has_current_caps (goom->srcpad)) {
476     if (!gst_goom_src_negotiate (goom))
477       return GST_FLOW_NOT_NEGOTIATED;
478   }
479   return GST_FLOW_OK;
480 }
481
482 static GstFlowReturn
483 gst_goom_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
484 {
485   GstGoom *goom;
486   GstFlowReturn ret;
487   GstBuffer *outbuf = NULL;
488
489   goom = GST_GOOM (parent);
490
491   /* If we don't have an output format yet, preallocate a buffer to try and
492    * set one */
493   if (goom->bps == 0) {
494     gst_buffer_unref (buffer);
495     ret = GST_FLOW_NOT_NEGOTIATED;
496     goto beach;
497   }
498
499   /* Make sure have an output format */
500   ret = ensure_negotiated (goom);
501   if (ret != GST_FLOW_OK) {
502     gst_buffer_unref (buffer);
503     goto beach;
504   }
505
506   /* don't try to combine samples from discont buffer */
507   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
508     gst_adapter_clear (goom->adapter);
509   }
510
511   GST_DEBUG_OBJECT (goom,
512       "Input buffer has %" G_GSIZE_FORMAT " samples, time=%" G_GUINT64_FORMAT,
513       gst_buffer_get_size (buffer) / goom->bps, GST_BUFFER_TIMESTAMP (buffer));
514
515   /* Collect samples until we have enough for an output frame */
516   gst_adapter_push (goom->adapter, buffer);
517
518   ret = GST_FLOW_OK;
519
520   while (TRUE) {
521     const guint16 *data;
522     gboolean need_skip;
523     guchar *out_frame;
524     gint i;
525     guint avail, to_flush;
526     guint64 dist, timestamp;
527
528     avail = gst_adapter_available (goom->adapter);
529     GST_DEBUG_OBJECT (goom, "avail now %u", avail);
530
531     /* we need GOOM_SAMPLES to get a meaningful result from goom. */
532     if (avail < (GOOM_SAMPLES * goom->bps))
533       break;
534
535     /* we also need enough samples to produce one frame at least */
536     if (avail < goom->bpf)
537       break;
538
539     GST_DEBUG_OBJECT (goom, "processing buffer");
540
541     /* get timestamp of the current adapter byte */
542     timestamp = gst_adapter_prev_timestamp (goom->adapter, &dist);
543     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
544       /* convert bytes to time */
545       dist /= goom->bps;
546       timestamp += gst_util_uint64_scale_int (dist, GST_SECOND, goom->rate);
547     }
548
549     if (timestamp != -1) {
550       gint64 qostime;
551
552       qostime = gst_segment_to_running_time (&goom->segment, GST_FORMAT_TIME,
553           timestamp);
554       qostime += goom->duration;
555
556       GST_OBJECT_LOCK (goom);
557       /* check for QoS, don't compute buffers that are known to be late */
558       need_skip = goom->earliest_time != -1 && qostime <= goom->earliest_time;
559       GST_OBJECT_UNLOCK (goom);
560
561       if (need_skip) {
562         GST_WARNING_OBJECT (goom,
563             "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
564             GST_TIME_ARGS (qostime), GST_TIME_ARGS (goom->earliest_time));
565         goto skip;
566       }
567     }
568
569     /* get next GOOM_SAMPLES, we have at least this amount of samples */
570     data =
571         (const guint16 *) gst_adapter_map (goom->adapter,
572         GOOM_SAMPLES * goom->bps);
573
574     if (goom->channels == 2) {
575       for (i = 0; i < GOOM_SAMPLES; i++) {
576         goom->datain[0][i] = *data++;
577         goom->datain[1][i] = *data++;
578       }
579     } else {
580       for (i = 0; i < GOOM_SAMPLES; i++) {
581         goom->datain[0][i] = *data;
582         goom->datain[1][i] = *data++;
583       }
584     }
585
586     /* alloc a buffer if we don't have one yet, this happens
587      * when we pushed a buffer in this while loop before */
588     if (outbuf == NULL) {
589       GST_DEBUG_OBJECT (goom, "allocating output buffer");
590       ret = gst_buffer_pool_acquire_buffer (goom->pool, &outbuf, NULL);
591       if (ret != GST_FLOW_OK) {
592         gst_adapter_unmap (goom->adapter);
593         goto beach;
594       }
595     }
596
597     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
598     GST_BUFFER_DURATION (outbuf) = goom->duration;
599
600     out_frame = (guchar *) goom_update (&(goom->goomdata), goom->datain);
601     gst_buffer_fill (outbuf, 0, out_frame, goom->outsize);
602
603     gst_adapter_unmap (goom->adapter);
604
605     GST_DEBUG ("Pushing frame with time=%" GST_TIME_FORMAT ", duration=%"
606         GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
607         GST_TIME_ARGS (goom->duration));
608
609     ret = gst_pad_push (goom->srcpad, outbuf);
610     outbuf = NULL;
611
612   skip:
613     /* Now flush the samples we needed for this frame, which might be more than
614      * the samples we used (GOOM_SAMPLES). */
615     to_flush = goom->bpf;
616
617     GST_DEBUG_OBJECT (goom, "finished frame, flushing %u bytes from input",
618         to_flush);
619     gst_adapter_flush (goom->adapter, to_flush);
620
621     if (ret != GST_FLOW_OK)
622       break;
623   }
624
625   if (outbuf != NULL)
626     gst_buffer_unref (outbuf);
627
628 beach:
629
630   return ret;
631 }
632
633 static GstStateChangeReturn
634 gst_goom_change_state (GstElement * element, GstStateChange transition)
635 {
636   GstGoom *goom = GST_GOOM (element);
637   GstStateChangeReturn ret;
638
639   switch (transition) {
640     case GST_STATE_CHANGE_NULL_TO_READY:
641       break;
642     case GST_STATE_CHANGE_READY_TO_PAUSED:
643       gst_goom_reset (goom);
644       break;
645     default:
646       break;
647   }
648
649   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
650
651   switch (transition) {
652     case GST_STATE_CHANGE_PAUSED_TO_READY:
653       if (goom->pool) {
654         gst_buffer_pool_set_active (goom->pool, FALSE);
655         gst_object_replace ((GstObject **) & goom->pool, NULL);
656       }
657       break;
658     case GST_STATE_CHANGE_READY_TO_NULL:
659       break;
660     default:
661       break;
662   }
663
664   return ret;
665 }
666
667 static gboolean
668 plugin_init (GstPlugin * plugin)
669 {
670   return gst_element_register (plugin, "goom2k1", GST_RANK_NONE, GST_TYPE_GOOM);
671 }
672
673 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
674     GST_VERSION_MINOR,
675     "goom2k1",
676     "GOOM 2k1 visualization filter",
677     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)