e61004450e8844551102e2b26f4c55458982e36b
[platform/upstream/gstreamer.git] / ext / libvisual / gstaudiobasevisualizer.c
1 /* GStreamer
2  * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstaudiobasevisualizer.h: base class for audio visualisation elements
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 /**
21  * SECTION:gstaudiobasevisualizer
22  *
23  * A basclass for scopes (visualizers). It takes care of re-fitting the
24  * audio-rate to video-rate and handles renegotiation (downstream video size
25  * changes).
26  * 
27  * It also provides several background shading effects. These effects are
28  * applied to a previous picture before the render() implementation can draw a
29  * new frame.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
37  * with newer GLib versions (>= 2.31.0) */
38 #define GLIB_DISABLE_DEPRECATION_WARNINGS
39
40 #include <string.h>
41
42 #include "gstaudiobasevisualizer.h"
43
44 GST_DEBUG_CATEGORY_STATIC (audio_base_visualizer_debug);
45 #define GST_CAT_DEFAULT (audio_base_visualizer_debug)
46
47 #define DEFAULT_SHADER GST_AUDIO_BASE_VISUALIZER_SHADER_FADE
48 #define DEFAULT_SHADE_AMOUNT   0x000a0a0a
49
50 enum
51 {
52   PROP_0,
53   PROP_SHADER,
54   PROP_SHADE_AMOUNT
55 };
56
57 static GstBaseTransformClass *parent_class = NULL;
58
59 static void gst_audio_base_visualizer_class_init (GstAudioBaseVisualizerClass *
60     klass);
61 static void gst_audio_base_visualizer_init (GstAudioBaseVisualizer * scope,
62     GstAudioBaseVisualizerClass * g_class);
63 static void gst_audio_base_visualizer_set_property (GObject * object,
64     guint prop_id, const GValue * value, GParamSpec * pspec);
65 static void gst_audio_base_visualizer_get_property (GObject * object,
66     guint prop_id, GValue * value, GParamSpec * pspec);
67 static void gst_audio_base_visualizer_dispose (GObject * object);
68
69 static gboolean gst_audio_base_visualizer_src_negotiate (GstAudioBaseVisualizer
70     * scope);
71 static gboolean gst_audio_base_visualizer_src_setcaps (GstAudioBaseVisualizer *
72     scope, GstCaps * caps);
73 static gboolean gst_audio_base_visualizer_sink_setcaps (GstAudioBaseVisualizer *
74     scope, GstCaps * caps);
75
76 static GstFlowReturn gst_audio_base_visualizer_chain (GstPad * pad,
77     GstObject * parent, GstBuffer * buffer);
78
79 static gboolean gst_audio_base_visualizer_src_event (GstPad * pad,
80     GstObject * parent, GstEvent * event);
81 static gboolean gst_audio_base_visualizer_sink_event (GstPad * pad,
82     GstObject * parent, GstEvent * event);
83
84 static gboolean gst_audio_base_visualizer_src_query (GstPad * pad,
85     GstObject * parent, GstQuery * query);
86 static gboolean gst_audio_base_visualizer_sink_query (GstPad * pad,
87     GstObject * parent, GstQuery * query);
88
89 static GstStateChangeReturn gst_audio_base_visualizer_change_state (GstElement *
90     element, GstStateChange transition);
91
92 /* shading functions */
93
94 #define GST_TYPE_AUDIO_BASE_VISUALIZER_SHADER (gst_audio_base_visualizer_shader_get_type())
95 static GType
96 gst_audio_base_visualizer_shader_get_type (void)
97 {
98   static GType shader_type = 0;
99   static const GEnumValue shaders[] = {
100     {GST_AUDIO_BASE_VISUALIZER_SHADER_NONE, "None", "none"},
101     {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE, "Fade", "fade"},
102     {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_UP, "Fade and move up",
103         "fade-and-move-up"},
104     {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN, "Fade and move down",
105         "fade-and-move-down"},
106     {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT, "Fade and move left",
107         "fade-and-move-left"},
108     {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT,
109           "Fade and move right",
110         "fade-and-move-right"},
111     {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT,
112         "Fade and move horizontally out", "fade-and-move-horiz-out"},
113     {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN,
114         "Fade and move horizontally in", "fade-and-move-horiz-in"},
115     {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT,
116         "Fade and move vertically out", "fade-and-move-vert-out"},
117     {GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_IN,
118         "Fade and move vertically in", "fade-and-move-vert-in"},
119     {0, NULL, NULL},
120   };
121
122   if (G_UNLIKELY (shader_type == 0)) {
123     shader_type =
124         g_enum_register_static ("GstAudioBaseVisualizerShader", shaders);
125   }
126   return shader_type;
127 }
128
129 /* we're only supporting GST_VIDEO_FORMAT_xRGB right now) */
130 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
131
132 #define SHADE1(_d, _s, _i, _r, _g, _b)          \
133 G_STMT_START {                                  \
134     _d[_i] = (_s[_i] > _b) ? _s[_i] - _b : 0;   \
135     _i++;                                       \
136     _d[_i] = (_s[_i] > _g) ? _s[_i] - _g : 0;   \
137     _i++;                                       \
138     _d[_i] = (_s[_i] > _r) ? _s[_i] - _r : 0;   \
139     _i++;                                       \
140     _d[_i++] = 0;                               \
141 } G_STMT_END
142
143 #define SHADE2(_d, _s, _j, _i, _r, _g, _b)      \
144 G_STMT_START {                                  \
145     _d[_j++] = (_s[_i] > _b) ? _s[_i] - _b : 0; \
146     _i++;                                       \
147     _d[_j++] = (_s[_i] > _g) ? _s[_i] - _g : 0; \
148     _i++;                                       \
149     _d[_j++] = (_s[_i] > _r) ? _s[_i] - _r : 0; \
150     _i++;                                       \
151     _d[_j++] = 0;                               \
152     _i++;                                       \
153 } G_STMT_END
154
155 #else
156
157 #define SHADE1(_d, _s, _i, _r, _g, _b)          \
158 G_STMT_START {                                  \
159     _d[_i++] = 0;                               \
160     _d[_i] = (_s[_i] > _r) ? _s[_i] - _r : 0;   \
161     _i++;                                       \
162     _d[_i] = (_s[_i] > _g) ? _s[_i] - _g : 0;   \
163     _i++;                                       \
164     _d[_i] = (_s[_i] > _b) ? _s[_i] - _b : 0;   \
165     _i++;                                       \
166 } G_STMT_END
167
168 #define SHADE2(_d, _s, _j, _i, _r, _g, _b)      \
169 G_STMT_START {                                  \
170     _d[_j++] = 0;                               \
171     _i++;                                       \
172     _d[_j++] = (_s[_i] > _r) ? _s[_i] - _r : 0; \
173     _i++;                                       \
174     _d[_j++] = (_s[_i] > _g) ? _s[_i] - _g : 0; \
175     _i++;                                       \
176     _d[_j++] = (_s[_i] > _b) ? _s[_i] - _b : 0; \
177     _i++;                                       \
178 } G_STMT_END
179
180 #endif
181
182 static void
183 shader_fade (GstAudioBaseVisualizer * scope, const guint8 * s, guint8 * d)
184 {
185   guint i, bpf = scope->bpf;
186   guint r = (scope->shade_amount >> 16) & 0xff;
187   guint g = (scope->shade_amount >> 8) & 0xff;
188   guint b = (scope->shade_amount >> 0) & 0xff;
189
190   for (i = 0; i < bpf;) {
191     SHADE1 (d, s, i, r, g, b);
192   }
193 }
194
195 static void
196 shader_fade_and_move_up (GstAudioBaseVisualizer * scope, const guint8 * s,
197     guint8 * d)
198 {
199   guint i, j, bpf = scope->bpf;
200   guint bpl = 4 * scope->width;
201   guint r = (scope->shade_amount >> 16) & 0xff;
202   guint g = (scope->shade_amount >> 8) & 0xff;
203   guint b = (scope->shade_amount >> 0) & 0xff;
204
205   for (j = 0, i = bpl; i < bpf;) {
206     SHADE2 (d, s, j, i, r, g, b);
207   }
208 }
209
210 static void
211 shader_fade_and_move_down (GstAudioBaseVisualizer * scope, const guint8 * s,
212     guint8 * d)
213 {
214   guint i, j, bpf = scope->bpf;
215   guint bpl = 4 * scope->width;
216   guint r = (scope->shade_amount >> 16) & 0xff;
217   guint g = (scope->shade_amount >> 8) & 0xff;
218   guint b = (scope->shade_amount >> 0) & 0xff;
219
220   for (j = bpl, i = 0; j < bpf;) {
221     SHADE2 (d, s, j, i, r, g, b);
222   }
223 }
224
225 static void
226 shader_fade_and_move_left (GstAudioBaseVisualizer * scope,
227     const guint8 * s, guint8 * d)
228 {
229   guint i, j, k, bpf = scope->bpf;
230   guint w = scope->width;
231   guint r = (scope->shade_amount >> 16) & 0xff;
232   guint g = (scope->shade_amount >> 8) & 0xff;
233   guint b = (scope->shade_amount >> 0) & 0xff;
234
235   /* move to the left */
236   for (j = 0, i = 4; i < bpf;) {
237     for (k = 0; k < w - 1; k++) {
238       SHADE2 (d, s, j, i, r, g, b);
239     }
240     i += 4;
241     j += 4;
242   }
243 }
244
245 static void
246 shader_fade_and_move_right (GstAudioBaseVisualizer * scope,
247     const guint8 * s, guint8 * d)
248 {
249   guint i, j, k, bpf = scope->bpf;
250   guint w = scope->width;
251   guint r = (scope->shade_amount >> 16) & 0xff;
252   guint g = (scope->shade_amount >> 8) & 0xff;
253   guint b = (scope->shade_amount >> 0) & 0xff;
254
255   /* move to the left */
256   for (j = 4, i = 0; i < bpf;) {
257     for (k = 0; k < w - 1; k++) {
258       SHADE2 (d, s, j, i, r, g, b);
259     }
260     i += 4;
261     j += 4;
262   }
263 }
264
265 static void
266 shader_fade_and_move_horiz_out (GstAudioBaseVisualizer * scope,
267     const guint8 * s, guint8 * d)
268 {
269   guint i, j, bpf = scope->bpf / 2;
270   guint bpl = 4 * scope->width;
271   guint r = (scope->shade_amount >> 16) & 0xff;
272   guint g = (scope->shade_amount >> 8) & 0xff;
273   guint b = (scope->shade_amount >> 0) & 0xff;
274
275   /* move upper half up */
276   for (j = 0, i = bpl; i < bpf;) {
277     SHADE2 (d, s, j, i, r, g, b);
278   }
279   /* move lower half down */
280   for (j = bpf + bpl, i = bpf; j < bpf + bpf;) {
281     SHADE2 (d, s, j, i, r, g, b);
282   }
283 }
284
285 static void
286 shader_fade_and_move_horiz_in (GstAudioBaseVisualizer * scope,
287     const guint8 * s, guint8 * d)
288 {
289   guint i, j, bpf = scope->bpf / 2;
290   guint bpl = 4 * scope->width;
291   guint r = (scope->shade_amount >> 16) & 0xff;
292   guint g = (scope->shade_amount >> 8) & 0xff;
293   guint b = (scope->shade_amount >> 0) & 0xff;
294
295   /* move upper half down */
296   for (i = 0, j = bpl; i < bpf;) {
297     SHADE2 (d, s, j, i, r, g, b);
298   }
299   /* move lower half up */
300   for (i = bpf + bpl, j = bpf; i < bpf + bpf;) {
301     SHADE2 (d, s, j, i, r, g, b);
302   }
303 }
304
305 static void
306 shader_fade_and_move_vert_out (GstAudioBaseVisualizer * scope,
307     const guint8 * s, guint8 * d)
308 {
309   guint i, j, k, bpf = scope->bpf;
310   guint m = scope->width / 2;
311   guint r = (scope->shade_amount >> 16) & 0xff;
312   guint g = (scope->shade_amount >> 8) & 0xff;
313   guint b = (scope->shade_amount >> 0) & 0xff;
314
315   /* move left half to the left */
316   for (j = 0, i = 4; i < bpf;) {
317     for (k = 0; k < m; k++) {
318       SHADE2 (d, s, j, i, r, g, b);
319     }
320     j += 4 * m;
321     i += 4 * m;
322   }
323   /* move right half to the right */
324   for (j = 4 * (m + 1), i = 4 * m; j < bpf;) {
325     for (k = 0; k < m; k++) {
326       SHADE2 (d, s, j, i, r, g, b);
327     }
328     j += 4 * m;
329     i += 4 * m;
330   }
331 }
332
333 static void
334 shader_fade_and_move_vert_in (GstAudioBaseVisualizer * scope,
335     const guint8 * s, guint8 * d)
336 {
337   guint i, j, k, bpf = scope->bpf;
338   guint m = scope->width / 2;
339   guint r = (scope->shade_amount >> 16) & 0xff;
340   guint g = (scope->shade_amount >> 8) & 0xff;
341   guint b = (scope->shade_amount >> 0) & 0xff;
342
343   /* move left half to the right */
344   for (j = 4, i = 0; j < bpf;) {
345     for (k = 0; k < m; k++) {
346       SHADE2 (d, s, j, i, r, g, b);
347     }
348     j += 4 * m;
349     i += 4 * m;
350   }
351   /* move right half to the left */
352   for (j = 4 * m, i = 4 * (m + 1); i < bpf;) {
353     for (k = 0; k < m; k++) {
354       SHADE2 (d, s, j, i, r, g, b);
355     }
356     j += 4 * m;
357     i += 4 * m;
358   }
359 }
360
361 static void
362 gst_audio_base_visualizer_change_shader (GstAudioBaseVisualizer * scope)
363 {
364   switch (scope->shader_type) {
365     case GST_AUDIO_BASE_VISUALIZER_SHADER_NONE:
366       scope->shader = NULL;
367       break;
368     case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE:
369       scope->shader = shader_fade;
370       break;
371     case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_UP:
372       scope->shader = shader_fade_and_move_up;
373       break;
374     case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN:
375       scope->shader = shader_fade_and_move_down;
376       break;
377     case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT:
378       scope->shader = shader_fade_and_move_left;
379       break;
380     case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT:
381       scope->shader = shader_fade_and_move_right;
382       break;
383     case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT:
384       scope->shader = shader_fade_and_move_horiz_out;
385       break;
386     case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN:
387       scope->shader = shader_fade_and_move_horiz_in;
388       break;
389     case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT:
390       scope->shader = shader_fade_and_move_vert_out;
391       break;
392     case GST_AUDIO_BASE_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_IN:
393       scope->shader = shader_fade_and_move_vert_in;
394       break;
395     default:
396       GST_ERROR ("invalid shader function");
397       scope->shader = NULL;
398       break;
399   }
400 }
401
402 /* base class */
403
404 GType
405 gst_audio_base_visualizer_get_type (void)
406 {
407   static volatile gsize audio_base_visualizer_type = 0;
408
409   if (g_once_init_enter (&audio_base_visualizer_type)) {
410     static const GTypeInfo audio_base_visualizer_info = {
411       sizeof (GstAudioBaseVisualizerClass),
412       NULL,
413       NULL,
414       (GClassInitFunc) gst_audio_base_visualizer_class_init,
415       NULL,
416       NULL,
417       sizeof (GstAudioBaseVisualizer),
418       0,
419       (GInstanceInitFunc) gst_audio_base_visualizer_init,
420     };
421     GType _type;
422
423     _type = g_type_register_static (GST_TYPE_ELEMENT,
424         "GstAudioBaseVisualizer", &audio_base_visualizer_info,
425         G_TYPE_FLAG_ABSTRACT);
426     g_once_init_leave (&audio_base_visualizer_type, _type);
427   }
428   return (GType) audio_base_visualizer_type;
429 }
430
431 static void
432 gst_audio_base_visualizer_class_init (GstAudioBaseVisualizerClass * klass)
433 {
434   GObjectClass *gobject_class = (GObjectClass *) klass;
435   GstElementClass *element_class = (GstElementClass *) klass;
436
437   parent_class = g_type_class_peek_parent (klass);
438
439   GST_DEBUG_CATEGORY_INIT (audio_base_visualizer_debug, "audiobasevisualizer",
440       0, "scope audio visualisation base class");
441
442   gobject_class->set_property = gst_audio_base_visualizer_set_property;
443   gobject_class->get_property = gst_audio_base_visualizer_get_property;
444   gobject_class->dispose = gst_audio_base_visualizer_dispose;
445
446   element_class->change_state =
447       GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_change_state);
448
449   g_object_class_install_property (gobject_class, PROP_SHADER,
450       g_param_spec_enum ("shader", "shader type",
451           "Shader function to apply on each frame",
452           GST_TYPE_AUDIO_BASE_VISUALIZER_SHADER, DEFAULT_SHADER,
453           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
454   g_object_class_install_property (gobject_class, PROP_SHADE_AMOUNT,
455       g_param_spec_uint ("shade-amount", "shade amount",
456           "Shading color to use (big-endian ARGB)", 0, G_MAXUINT32,
457           DEFAULT_SHADE_AMOUNT,
458           G_PARAM_READWRITE | GST_PARAM_CONTROLLABLE | G_PARAM_STATIC_STRINGS));
459 }
460
461 static void
462 gst_audio_base_visualizer_init (GstAudioBaseVisualizer * scope,
463     GstAudioBaseVisualizerClass * g_class)
464 {
465   GstPadTemplate *pad_template;
466
467   /* create the sink and src pads */
468   pad_template =
469       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
470   g_return_if_fail (pad_template != NULL);
471   scope->sinkpad = gst_pad_new_from_template (pad_template, "sink");
472   gst_pad_set_chain_function (scope->sinkpad,
473       GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_chain));
474   gst_pad_set_event_function (scope->sinkpad,
475       GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_sink_event));
476   gst_pad_set_query_function (scope->sinkpad,
477       GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_sink_query));
478   gst_element_add_pad (GST_ELEMENT (scope), scope->sinkpad);
479
480   pad_template =
481       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "src");
482   g_return_if_fail (pad_template != NULL);
483   scope->srcpad = gst_pad_new_from_template (pad_template, "src");
484   gst_pad_set_event_function (scope->srcpad,
485       GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_src_event));
486   gst_pad_set_query_function (scope->srcpad,
487       GST_DEBUG_FUNCPTR (gst_audio_base_visualizer_src_query));
488   gst_element_add_pad (GST_ELEMENT (scope), scope->srcpad);
489
490   scope->adapter = gst_adapter_new ();
491   scope->inbuf = gst_buffer_new ();
492
493   /* properties */
494   scope->shader_type = DEFAULT_SHADER;
495   gst_audio_base_visualizer_change_shader (scope);
496   scope->shade_amount = DEFAULT_SHADE_AMOUNT;
497
498   /* reset the initial video state */
499   scope->width = 320;
500   scope->height = 200;
501   scope->fps_n = 25;            /* desired frame rate */
502   scope->fps_d = 1;
503   scope->frame_duration = GST_CLOCK_TIME_NONE;
504
505   /* reset the initial state */
506   gst_audio_info_init (&scope->ainfo);
507   gst_video_info_init (&scope->vinfo);
508
509   g_mutex_init (&scope->config_lock);
510 }
511
512 static void
513 gst_audio_base_visualizer_set_property (GObject * object, guint prop_id,
514     const GValue * value, GParamSpec * pspec)
515 {
516   GstAudioBaseVisualizer *scope = GST_AUDIO_BASE_VISUALIZER (object);
517
518   switch (prop_id) {
519     case PROP_SHADER:
520       scope->shader_type = g_value_get_enum (value);
521       gst_audio_base_visualizer_change_shader (scope);
522       break;
523     case PROP_SHADE_AMOUNT:
524       scope->shade_amount = g_value_get_uint (value);
525       break;
526     default:
527       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
528       break;
529   }
530 }
531
532 static void
533 gst_audio_base_visualizer_get_property (GObject * object, guint prop_id,
534     GValue * value, GParamSpec * pspec)
535 {
536   GstAudioBaseVisualizer *scope = GST_AUDIO_BASE_VISUALIZER (object);
537
538   switch (prop_id) {
539     case PROP_SHADER:
540       g_value_set_enum (value, scope->shader_type);
541       break;
542     case PROP_SHADE_AMOUNT:
543       g_value_set_uint (value, scope->shade_amount);
544       break;
545     default:
546       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
547       break;
548   }
549 }
550
551 static void
552 gst_audio_base_visualizer_dispose (GObject * object)
553 {
554   GstAudioBaseVisualizer *scope = GST_AUDIO_BASE_VISUALIZER (object);
555
556   if (scope->adapter) {
557     g_object_unref (scope->adapter);
558     scope->adapter = NULL;
559   }
560   if (scope->inbuf) {
561     gst_buffer_unref (scope->inbuf);
562     scope->inbuf = NULL;
563   }
564   if (scope->pixelbuf) {
565     g_free (scope->pixelbuf);
566     scope->pixelbuf = NULL;
567   }
568   if (scope->config_lock.p) {
569     g_mutex_clear (&scope->config_lock);
570     scope->config_lock.p = NULL;
571   }
572   G_OBJECT_CLASS (parent_class)->dispose (object);
573 }
574
575 static void
576 gst_audio_base_visualizer_reset (GstAudioBaseVisualizer * scope)
577 {
578   gst_adapter_clear (scope->adapter);
579   gst_segment_init (&scope->segment, GST_FORMAT_UNDEFINED);
580
581   GST_OBJECT_LOCK (scope);
582   scope->proportion = 1.0;
583   scope->earliest_time = -1;
584   GST_OBJECT_UNLOCK (scope);
585 }
586
587 static gboolean
588 gst_audio_base_visualizer_sink_setcaps (GstAudioBaseVisualizer * scope,
589     GstCaps * caps)
590 {
591   GstAudioInfo info;
592   gboolean res = TRUE;
593
594   if (!gst_audio_info_from_caps (&info, caps))
595     goto wrong_caps;
596
597   scope->ainfo = info;
598
599   GST_DEBUG_OBJECT (scope, "audio: channels %d, rate %d",
600       GST_AUDIO_INFO_CHANNELS (&info), GST_AUDIO_INFO_RATE (&info));
601
602 done:
603   return res;
604
605   /* Errors */
606 wrong_caps:
607   {
608     GST_WARNING_OBJECT (scope, "could not parse caps");
609     res = FALSE;
610     goto done;
611   }
612 }
613
614 static gboolean
615 gst_audio_base_visualizer_src_setcaps (GstAudioBaseVisualizer * scope,
616     GstCaps * caps)
617 {
618   GstVideoInfo info;
619   GstAudioBaseVisualizerClass *klass;
620   GstStructure *structure;
621   gboolean res;
622
623   if (!gst_video_info_from_caps (&info, caps))
624     goto wrong_caps;
625
626   structure = gst_caps_get_structure (caps, 0);
627   if (!gst_structure_get_int (structure, "width", &scope->width) ||
628       !gst_structure_get_int (structure, "height", &scope->height) ||
629       !gst_structure_get_fraction (structure, "framerate", &scope->fps_n,
630           &scope->fps_d))
631     goto wrong_caps;
632
633   klass = GST_AUDIO_BASE_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
634
635   scope->vinfo = info;
636   scope->video_format = info.finfo->format;
637
638   scope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
639       scope->fps_d, scope->fps_n);
640   scope->spf = gst_util_uint64_scale_int (GST_AUDIO_INFO_RATE (&scope->ainfo),
641       scope->fps_d, scope->fps_n);
642   scope->req_spf = scope->spf;
643
644   scope->bpf = scope->width * scope->height * 4;
645
646   if (scope->pixelbuf)
647     g_free (scope->pixelbuf);
648   scope->pixelbuf = g_malloc0 (scope->bpf);
649
650   if (klass->setup)
651     res = klass->setup (scope);
652
653   GST_DEBUG_OBJECT (scope, "video: dimension %dx%d, framerate %d/%d",
654       scope->width, scope->height, scope->fps_n, scope->fps_d);
655   GST_DEBUG_OBJECT (scope, "blocks: spf %u, req_spf %u",
656       scope->spf, scope->req_spf);
657
658   res = gst_pad_set_caps (scope->srcpad, caps);
659
660   return res;
661
662   /* ERRORS */
663 wrong_caps:
664   {
665     GST_DEBUG_OBJECT (scope, "error parsing caps");
666     return FALSE;
667   }
668 }
669
670 static gboolean
671 gst_audio_base_visualizer_src_negotiate (GstAudioBaseVisualizer * scope)
672 {
673   GstCaps *othercaps, *target;
674   GstStructure *structure;
675   GstCaps *templ;
676   GstQuery *query;
677   GstBufferPool *pool;
678   GstStructure *config;
679   guint size, min, max;
680
681   templ = gst_pad_get_pad_template_caps (scope->srcpad);
682
683   GST_DEBUG_OBJECT (scope, "performing negotiation");
684
685   /* see what the peer can do */
686   othercaps = gst_pad_peer_query_caps (scope->srcpad, NULL);
687   if (othercaps) {
688     target = gst_caps_intersect (othercaps, templ);
689     gst_caps_unref (othercaps);
690     gst_caps_unref (templ);
691
692     if (gst_caps_is_empty (target))
693       goto no_format;
694
695     target = gst_caps_truncate (target);
696   } else {
697     target = templ;
698   }
699
700   target = gst_caps_make_writable (target);
701   structure = gst_caps_get_structure (target, 0);
702   gst_structure_fixate_field_nearest_int (structure, "width", scope->width);
703   gst_structure_fixate_field_nearest_int (structure, "height", scope->height);
704   gst_structure_fixate_field_nearest_fraction (structure, "framerate",
705       scope->fps_n, scope->fps_d);
706
707   GST_DEBUG_OBJECT (scope, "final caps are %" GST_PTR_FORMAT, target);
708
709   gst_audio_base_visualizer_src_setcaps (scope, target);
710
711   /* try to get a bufferpool now */
712   /* find a pool for the negotiated caps now */
713   query = gst_query_new_allocation (target, TRUE);
714
715   if (!gst_pad_peer_query (scope->srcpad, query)) {
716     /* not a problem, we use the query defaults */
717     GST_DEBUG_OBJECT (scope, "allocation query failed");
718   }
719
720   if (gst_query_get_n_allocation_pools (query) > 0) {
721     /* we got configuration from our peer, parse them */
722     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
723   } else {
724     pool = NULL;
725     size = scope->bpf;
726     min = max = 0;
727   }
728
729   if (pool == NULL) {
730     /* we did not get a pool, make one ourselves then */
731     pool = gst_buffer_pool_new ();
732   }
733
734   config = gst_buffer_pool_get_config (pool);
735   gst_buffer_pool_config_set_params (config, target, size, min, max);
736   gst_buffer_pool_set_config (pool, config);
737
738   if (scope->pool) {
739     gst_buffer_pool_set_active (scope->pool, FALSE);
740     gst_object_unref (scope->pool);
741   }
742   scope->pool = pool;
743
744   /* and activate */
745   gst_buffer_pool_set_active (pool, TRUE);
746
747   gst_caps_unref (target);
748
749   return TRUE;
750
751 no_format:
752   {
753     gst_caps_unref (target);
754     return FALSE;
755   }
756 }
757
758 /* make sure we are negotiated */
759 static GstFlowReturn
760 gst_audio_base_visualizer_ensure_negotiated (GstAudioBaseVisualizer * scope)
761 {
762   gboolean reconfigure;
763
764   reconfigure = gst_pad_check_reconfigure (scope->srcpad);
765
766   /* we don't know an output format yet, pick one */
767   if (reconfigure || !gst_pad_has_current_caps (scope->srcpad)) {
768     if (!gst_audio_base_visualizer_src_negotiate (scope))
769       return GST_FLOW_NOT_NEGOTIATED;
770   }
771   return GST_FLOW_OK;
772 }
773
774 static GstFlowReturn
775 gst_audio_base_visualizer_chain (GstPad * pad, GstObject * parent,
776     GstBuffer * buffer)
777 {
778   GstFlowReturn ret = GST_FLOW_OK;
779   GstAudioBaseVisualizer *scope;
780   GstAudioBaseVisualizerClass *klass;
781   GstBuffer *inbuf;
782   guint64 dist, ts;
783   guint avail, sbpf;
784   gpointer adata;
785   gboolean (*render) (GstAudioBaseVisualizer * scope, GstBuffer * audio,
786       GstBuffer * video);
787   gint bps, channels, rate;
788
789   scope = GST_AUDIO_BASE_VISUALIZER (parent);
790   klass = GST_AUDIO_BASE_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
791
792   render = klass->render;
793
794   GST_LOG_OBJECT (scope, "chainfunc called");
795
796   /* resync on DISCONT */
797   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
798     gst_adapter_clear (scope->adapter);
799   }
800
801   /* Make sure have an output format */
802   ret = gst_audio_base_visualizer_ensure_negotiated (scope);
803   if (ret != GST_FLOW_OK) {
804     gst_buffer_unref (buffer);
805     goto beach;
806   }
807   channels = GST_AUDIO_INFO_CHANNELS (&scope->ainfo);
808   rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
809   bps = GST_AUDIO_INFO_BPS (&scope->ainfo);
810
811   if (bps == 0) {
812     ret = GST_FLOW_NOT_NEGOTIATED;
813     goto beach;
814   }
815
816   gst_adapter_push (scope->adapter, buffer);
817
818   g_mutex_lock (&scope->config_lock);
819
820   /* this is what we want */
821   sbpf = scope->req_spf * channels * sizeof (gint16);
822
823   inbuf = scope->inbuf;
824   /* FIXME: the timestamp in the adapter would be different */
825   gst_buffer_copy_into (inbuf, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
826
827   /* this is what we have */
828   avail = gst_adapter_available (scope->adapter);
829   GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
830   while (avail >= sbpf) {
831     GstBuffer *outbuf;
832     GstMapInfo map;
833
834     /* get timestamp of the current adapter content */
835     ts = gst_adapter_prev_timestamp (scope->adapter, &dist);
836     if (GST_CLOCK_TIME_IS_VALID (ts)) {
837       /* convert bytes to time */
838       dist /= bps;
839       ts += gst_util_uint64_scale_int (dist, GST_SECOND, rate);
840     }
841
842     if (GST_CLOCK_TIME_IS_VALID (ts)) {
843       gint64 qostime;
844       gboolean need_skip;
845
846       qostime =
847           gst_segment_to_running_time (&scope->segment, GST_FORMAT_TIME, ts) +
848           scope->frame_duration;
849
850       GST_OBJECT_LOCK (scope);
851       /* check for QoS, don't compute buffers that are known to be late */
852       need_skip = scope->earliest_time != -1 && qostime <= scope->earliest_time;
853       GST_OBJECT_UNLOCK (scope);
854
855       if (need_skip) {
856         GST_WARNING_OBJECT (scope,
857             "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
858             GST_TIME_ARGS (qostime), GST_TIME_ARGS (scope->earliest_time));
859         goto skip;
860       }
861     }
862
863     g_mutex_unlock (&scope->config_lock);
864     ret = gst_buffer_pool_acquire_buffer (scope->pool, &outbuf, NULL);
865     g_mutex_lock (&scope->config_lock);
866     /* recheck as the value could have changed */
867     sbpf = scope->req_spf * channels * sizeof (gint16);
868
869     /* no buffer allocated, we don't care why. */
870     if (ret != GST_FLOW_OK)
871       break;
872
873     /* sync controlled properties */
874     gst_object_sync_values (GST_OBJECT (scope), ts);
875
876     GST_BUFFER_TIMESTAMP (outbuf) = ts;
877     GST_BUFFER_DURATION (outbuf) = scope->frame_duration;
878
879     gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
880     if (scope->shader) {
881       memcpy (map.data, scope->pixelbuf, scope->bpf);
882     } else {
883       memset (map.data, 0, scope->bpf);
884     }
885
886     /* this can fail as the data size we need could have changed */
887     if (!(adata = (gpointer) gst_adapter_map (scope->adapter, sbpf)))
888       break;
889
890     gst_buffer_replace_all_memory (inbuf,
891         gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, adata, sbpf, 0,
892             sbpf, NULL, NULL));
893
894     /* call class->render() vmethod */
895     if (render) {
896       if (!render (scope, inbuf, outbuf)) {
897         ret = GST_FLOW_ERROR;
898       } else {
899         /* run various post processing (shading and geometri transformation */
900         if (scope->shader) {
901           scope->shader (scope, map.data, scope->pixelbuf);
902         }
903       }
904     }
905
906     gst_buffer_unmap (outbuf, &map);
907     gst_buffer_resize (outbuf, 0, scope->bpf);
908
909     g_mutex_unlock (&scope->config_lock);
910     ret = gst_pad_push (scope->srcpad, outbuf);
911     outbuf = NULL;
912     g_mutex_lock (&scope->config_lock);
913
914   skip:
915     /* recheck as the value could have changed */
916     sbpf = scope->req_spf * channels * sizeof (gint16);
917     GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
918     /* we want to take less or more, depending on spf : req_spf */
919     if (avail - sbpf >= sbpf) {
920       gst_adapter_flush (scope->adapter, sbpf);
921       gst_adapter_unmap (scope->adapter);
922     } else if (avail >= sbpf) {
923       /* just flush a bit and stop */
924       gst_adapter_flush (scope->adapter, (avail - sbpf));
925       gst_adapter_unmap (scope->adapter);
926       break;
927     }
928     avail = gst_adapter_available (scope->adapter);
929
930     if (ret != GST_FLOW_OK)
931       break;
932   }
933
934   g_mutex_unlock (&scope->config_lock);
935
936 beach:
937   return ret;
938 }
939
940 static gboolean
941 gst_audio_base_visualizer_src_event (GstPad * pad, GstObject * parent,
942     GstEvent * event)
943 {
944   gboolean res;
945   GstAudioBaseVisualizer *scope;
946
947   scope = GST_AUDIO_BASE_VISUALIZER (parent);
948
949   switch (GST_EVENT_TYPE (event)) {
950     case GST_EVENT_QOS:
951     {
952       gdouble proportion;
953       GstClockTimeDiff diff;
954       GstClockTime timestamp;
955
956       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
957
958       /* save stuff for the _chain() function */
959       GST_OBJECT_LOCK (scope);
960       scope->proportion = proportion;
961       if (diff >= 0)
962         /* we're late, this is a good estimate for next displayable
963          * frame (see part-qos.txt) */
964         scope->earliest_time = timestamp + 2 * diff + scope->frame_duration;
965       else
966         scope->earliest_time = timestamp + diff;
967       GST_OBJECT_UNLOCK (scope);
968
969       res = gst_pad_push_event (scope->sinkpad, event);
970       break;
971     }
972     case GST_EVENT_RECONFIGURE:
973       /* dont't forward */
974       gst_event_unref (event);
975       res = TRUE;
976       break;
977     default:
978       res = gst_pad_push_event (scope->sinkpad, event);
979       break;
980   }
981
982   return res;
983 }
984
985 static gboolean
986 gst_audio_base_visualizer_sink_event (GstPad * pad, GstObject * parent,
987     GstEvent * event)
988 {
989   gboolean res;
990   GstAudioBaseVisualizer *scope;
991
992   scope = GST_AUDIO_BASE_VISUALIZER (parent);
993
994   switch (GST_EVENT_TYPE (event)) {
995     case GST_EVENT_CAPS:
996     {
997       GstCaps *caps;
998
999       gst_event_parse_caps (event, &caps);
1000       res = gst_audio_base_visualizer_sink_setcaps (scope, caps);
1001       break;
1002     }
1003     case GST_EVENT_FLUSH_START:
1004       res = gst_pad_push_event (scope->srcpad, event);
1005       break;
1006     case GST_EVENT_FLUSH_STOP:
1007       gst_audio_base_visualizer_reset (scope);
1008       res = gst_pad_push_event (scope->srcpad, event);
1009       break;
1010     case GST_EVENT_SEGMENT:
1011     {
1012       /* the newsegment values are used to clip the input samples
1013        * and to convert the incomming timestamps to running time so
1014        * we can do QoS */
1015       gst_event_copy_segment (event, &scope->segment);
1016
1017       res = gst_pad_push_event (scope->srcpad, event);
1018       break;
1019     }
1020     default:
1021       res = gst_pad_push_event (scope->srcpad, event);
1022       break;
1023   }
1024
1025   return res;
1026 }
1027
1028 static gboolean
1029 gst_audio_base_visualizer_src_query (GstPad * pad, GstObject * parent,
1030     GstQuery * query)
1031 {
1032   gboolean res = FALSE;
1033   GstAudioBaseVisualizer *scope;
1034
1035   scope = GST_AUDIO_BASE_VISUALIZER (parent);
1036
1037   switch (GST_QUERY_TYPE (query)) {
1038     case GST_QUERY_LATENCY:
1039     {
1040       /* We need to send the query upstream and add the returned latency to our
1041        * own */
1042       GstClockTime min_latency, max_latency;
1043       gboolean us_live;
1044       GstClockTime our_latency;
1045       guint max_samples;
1046       gint rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
1047
1048       if (rate == 0)
1049         break;
1050
1051       if ((res = gst_pad_peer_query (scope->sinkpad, query))) {
1052         gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
1053
1054         GST_DEBUG_OBJECT (scope, "Peer latency: min %"
1055             GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1056             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1057
1058         /* the max samples we must buffer buffer */
1059         max_samples = MAX (scope->req_spf, scope->spf);
1060         our_latency = gst_util_uint64_scale_int (max_samples, GST_SECOND, rate);
1061
1062         GST_DEBUG_OBJECT (scope, "Our latency: %" GST_TIME_FORMAT,
1063             GST_TIME_ARGS (our_latency));
1064
1065         /* we add some latency but only if we need to buffer more than what
1066          * upstream gives us */
1067         min_latency += our_latency;
1068         if (max_latency != -1)
1069           max_latency += our_latency;
1070
1071         GST_DEBUG_OBJECT (scope, "Calculated total latency : min %"
1072             GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1073             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1074
1075         gst_query_set_latency (query, TRUE, min_latency, max_latency);
1076       }
1077       break;
1078     }
1079     default:
1080       res = gst_pad_query_default (pad, parent, query);
1081       break;
1082   }
1083
1084   return res;
1085 }
1086
1087 static gboolean
1088 gst_audio_base_visualizer_sink_query (GstPad * pad, GstObject * parent,
1089     GstQuery * query)
1090 {
1091   gboolean res = FALSE;
1092
1093   switch (GST_QUERY_TYPE (query)) {
1094     default:
1095       res = gst_pad_query_default (pad, parent, query);
1096       break;
1097   }
1098   return res;
1099 }
1100
1101 static GstStateChangeReturn
1102 gst_audio_base_visualizer_change_state (GstElement * element,
1103     GstStateChange transition)
1104 {
1105   GstStateChangeReturn ret;
1106   GstAudioBaseVisualizer *scope;
1107
1108   scope = GST_AUDIO_BASE_VISUALIZER (element);
1109
1110   switch (transition) {
1111     case GST_STATE_CHANGE_READY_TO_PAUSED:
1112       gst_audio_base_visualizer_reset (scope);
1113       break;
1114     default:
1115       break;
1116   }
1117
1118   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1119
1120   switch (transition) {
1121     case GST_STATE_CHANGE_PAUSED_TO_READY:
1122       if (scope->pool) {
1123         gst_buffer_pool_set_active (scope->pool, FALSE);
1124         gst_object_replace ((GstObject **) & scope->pool, NULL);
1125       }
1126       break;
1127     case GST_STATE_CHANGE_READY_TO_NULL:
1128       break;
1129     default:
1130       break;
1131   }
1132
1133   return ret;
1134 }