9d68ca88ccf7bb34f14fe6f0a451e5254ccccaee
[platform/upstream/gstreamer.git] / gst / audiovisualizers / gstbaseaudiovisualizer.c
1 /* GStreamer
2  * Copyright (C) <2011> Stefan Kost <ensonic@users.sf.net>
3  *
4  * gstbaseaudiovisualizer.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:gstbaseaudiovisualizer
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 "gstbaseaudiovisualizer.h"
43
44 GST_DEBUG_CATEGORY_STATIC (base_audio_visualizer_debug);
45 #define GST_CAT_DEFAULT (base_audio_visualizer_debug)
46
47 #define DEFAULT_SHADER GST_BASE_AUDIO_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_base_audio_visualizer_class_init (GstBaseAudioVisualizerClass *
60     klass);
61 static void gst_base_audio_visualizer_init (GstBaseAudioVisualizer * scope,
62     GstBaseAudioVisualizerClass * g_class);
63 static void gst_base_audio_visualizer_set_property (GObject * object,
64     guint prop_id, const GValue * value, GParamSpec * pspec);
65 static void gst_base_audio_visualizer_get_property (GObject * object,
66     guint prop_id, GValue * value, GParamSpec * pspec);
67 static void gst_base_audio_visualizer_dispose (GObject * object);
68
69 static gboolean gst_base_audio_visualizer_src_negotiate (GstBaseAudioVisualizer
70     * scope);
71 static gboolean gst_base_audio_visualizer_src_setcaps (GstBaseAudioVisualizer *
72     scope, GstCaps * caps);
73 static gboolean gst_base_audio_visualizer_sink_setcaps (GstBaseAudioVisualizer *
74     scope, GstCaps * caps);
75
76 static GstFlowReturn gst_base_audio_visualizer_chain (GstPad * pad,
77     GstObject * parent, GstBuffer * buffer);
78
79 static gboolean gst_base_audio_visualizer_src_event (GstPad * pad,
80     GstObject * parent, GstEvent * event);
81 static gboolean gst_base_audio_visualizer_sink_event (GstPad * pad,
82     GstObject * parent, GstEvent * event);
83
84 static gboolean gst_base_audio_visualizer_src_query (GstPad * pad,
85     GstObject * parent, GstQuery * query);
86 static gboolean gst_base_audio_visualizer_sink_query (GstPad * pad,
87     GstObject * parent, GstQuery * query);
88
89 static GstStateChangeReturn gst_base_audio_visualizer_change_state (GstElement *
90     element, GstStateChange transition);
91
92 /* shading functions */
93
94 #define GST_TYPE_BASE_AUDIO_VISUALIZER_SHADER (gst_base_audio_visualizer_shader_get_type())
95 static GType
96 gst_base_audio_visualizer_shader_get_type (void)
97 {
98   static GType shader_type = 0;
99   static const GEnumValue shaders[] = {
100     {GST_BASE_AUDIO_VISUALIZER_SHADER_NONE, "None", "none"},
101     {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE, "Fade", "fade"},
102     {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_UP, "Fade and move up",
103         "fade-and-move-up"},
104     {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN, "Fade and move down",
105         "fade-and-move-down"},
106     {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT, "Fade and move left",
107         "fade-and-move-left"},
108     {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT,
109           "Fade and move right",
110         "fade-and-move-right"},
111     {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT,
112         "Fade and move horizontally out", "fade-and-move-horiz-out"},
113     {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN,
114         "Fade and move horizontally in", "fade-and-move-horiz-in"},
115     {GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT,
116         "Fade and move vertically out", "fade-and-move-vert-out"},
117     {GST_BASE_AUDIO_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 ("GstBaseAudioVisualizerShader", 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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 (GstBaseAudioVisualizer * 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_base_audio_visualizer_change_shader (GstBaseAudioVisualizer * scope)
363 {
364   switch (scope->shader_type) {
365     case GST_BASE_AUDIO_VISUALIZER_SHADER_NONE:
366       scope->shader = NULL;
367       break;
368     case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE:
369       scope->shader = shader_fade;
370       break;
371     case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_UP:
372       scope->shader = shader_fade_and_move_up;
373       break;
374     case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_DOWN:
375       scope->shader = shader_fade_and_move_down;
376       break;
377     case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_LEFT:
378       scope->shader = shader_fade_and_move_left;
379       break;
380     case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_RIGHT:
381       scope->shader = shader_fade_and_move_right;
382       break;
383     case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_OUT:
384       scope->shader = shader_fade_and_move_horiz_out;
385       break;
386     case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_HORIZ_IN:
387       scope->shader = shader_fade_and_move_horiz_in;
388       break;
389     case GST_BASE_AUDIO_VISUALIZER_SHADER_FADE_AND_MOVE_VERT_OUT:
390       scope->shader = shader_fade_and_move_vert_out;
391       break;
392     case GST_BASE_AUDIO_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_base_audio_visualizer_get_type (void)
406 {
407   static volatile gsize base_audio_visualizer_type = 0;
408
409   if (g_once_init_enter (&base_audio_visualizer_type)) {
410     static const GTypeInfo base_audio_visualizer_info = {
411       sizeof (GstBaseAudioVisualizerClass),
412       NULL,
413       NULL,
414       (GClassInitFunc) gst_base_audio_visualizer_class_init,
415       NULL,
416       NULL,
417       sizeof (GstBaseAudioVisualizer),
418       0,
419       (GInstanceInitFunc) gst_base_audio_visualizer_init,
420     };
421     GType _type;
422
423     _type = g_type_register_static (GST_TYPE_ELEMENT,
424         "GstBaseAudioVisualizer", &base_audio_visualizer_info,
425         G_TYPE_FLAG_ABSTRACT);
426     g_once_init_leave (&base_audio_visualizer_type, _type);
427   }
428   return (GType) base_audio_visualizer_type;
429 }
430
431 static void
432 gst_base_audio_visualizer_class_init (GstBaseAudioVisualizerClass * 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 (base_audio_visualizer_debug, "baseaudiovisualizer",
440       0, "scope audio visualisation base class");
441
442   gobject_class->set_property = gst_base_audio_visualizer_set_property;
443   gobject_class->get_property = gst_base_audio_visualizer_get_property;
444   gobject_class->dispose = gst_base_audio_visualizer_dispose;
445
446   element_class->change_state =
447       GST_DEBUG_FUNCPTR (gst_base_audio_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_BASE_AUDIO_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_base_audio_visualizer_init (GstBaseAudioVisualizer * scope,
463     GstBaseAudioVisualizerClass * 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_base_audio_visualizer_chain));
474   gst_pad_set_event_function (scope->sinkpad,
475       GST_DEBUG_FUNCPTR (gst_base_audio_visualizer_sink_event));
476   gst_pad_set_query_function (scope->sinkpad,
477       GST_DEBUG_FUNCPTR (gst_base_audio_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_base_audio_visualizer_src_event));
486   gst_pad_set_query_function (scope->srcpad,
487       GST_DEBUG_FUNCPTR (gst_base_audio_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_base_audio_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 audio state */
506   gst_audio_info_init (&scope->ainfo);
507
508   g_mutex_init (&scope->config_lock);
509 }
510
511 static void
512 gst_base_audio_visualizer_set_property (GObject * object, guint prop_id,
513     const GValue * value, GParamSpec * pspec)
514 {
515   GstBaseAudioVisualizer *scope = GST_BASE_AUDIO_VISUALIZER (object);
516
517   switch (prop_id) {
518     case PROP_SHADER:
519       scope->shader_type = g_value_get_enum (value);
520       gst_base_audio_visualizer_change_shader (scope);
521       break;
522     case PROP_SHADE_AMOUNT:
523       scope->shade_amount = g_value_get_uint (value);
524       break;
525     default:
526       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
527       break;
528   }
529 }
530
531 static void
532 gst_base_audio_visualizer_get_property (GObject * object, guint prop_id,
533     GValue * value, GParamSpec * pspec)
534 {
535   GstBaseAudioVisualizer *scope = GST_BASE_AUDIO_VISUALIZER (object);
536
537   switch (prop_id) {
538     case PROP_SHADER:
539       g_value_set_enum (value, scope->shader_type);
540       break;
541     case PROP_SHADE_AMOUNT:
542       g_value_set_uint (value, scope->shade_amount);
543       break;
544     default:
545       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
546       break;
547   }
548 }
549
550 static void
551 gst_base_audio_visualizer_dispose (GObject * object)
552 {
553   GstBaseAudioVisualizer *scope = GST_BASE_AUDIO_VISUALIZER (object);
554
555   if (scope->adapter) {
556     g_object_unref (scope->adapter);
557     scope->adapter = NULL;
558   }
559   if (scope->inbuf) {
560     gst_buffer_unref (scope->inbuf);
561     scope->inbuf = NULL;
562   }
563   if (scope->pixelbuf) {
564     g_free (scope->pixelbuf);
565     scope->pixelbuf = NULL;
566   }
567   if (scope->config_lock.p) {
568     g_mutex_clear (&scope->config_lock);
569     scope->config_lock.p = NULL;
570   }
571   G_OBJECT_CLASS (parent_class)->dispose (object);
572 }
573
574 static void
575 gst_base_audio_visualizer_reset (GstBaseAudioVisualizer * scope)
576 {
577   gst_adapter_clear (scope->adapter);
578   gst_segment_init (&scope->segment, GST_FORMAT_UNDEFINED);
579
580   GST_OBJECT_LOCK (scope);
581   scope->proportion = 1.0;
582   scope->earliest_time = -1;
583   GST_OBJECT_UNLOCK (scope);
584 }
585
586 static gboolean
587 gst_base_audio_visualizer_sink_setcaps (GstBaseAudioVisualizer * scope,
588     GstCaps * caps)
589 {
590   GstAudioInfo info;
591   gboolean res = TRUE;
592
593   if (!gst_audio_info_from_caps (&info, caps))
594     goto wrong_caps;
595
596   scope->ainfo = info;
597
598   GST_DEBUG_OBJECT (scope, "audio: channels %d, rate %d",
599       GST_AUDIO_INFO_CHANNELS (&info), GST_AUDIO_INFO_RATE (&info));
600
601 done:
602   return res;
603
604   /* Errors */
605 wrong_caps:
606   {
607     GST_WARNING_OBJECT (scope, "could not parse caps");
608     res = FALSE;
609     goto done;
610   }
611 }
612
613 static gboolean
614 gst_base_audio_visualizer_src_setcaps (GstBaseAudioVisualizer * scope,
615     GstCaps * caps)
616 {
617   GstBaseAudioVisualizerClass *klass;
618   GstStructure *structure;
619   gboolean res;
620
621   structure = gst_caps_get_structure (caps, 0);
622   if (!gst_structure_get_int (structure, "width", &scope->width) ||
623       !gst_structure_get_int (structure, "height", &scope->height) ||
624       !gst_structure_get_fraction (structure, "framerate", &scope->fps_n,
625           &scope->fps_d))
626     goto error;
627
628   klass = GST_BASE_AUDIO_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
629
630   //scope->video_format = format; ??
631
632   scope->frame_duration = gst_util_uint64_scale_int (GST_SECOND,
633       scope->fps_d, scope->fps_n);
634   scope->spf = gst_util_uint64_scale_int (GST_AUDIO_INFO_RATE (&scope->ainfo),
635       scope->fps_d, scope->fps_n);
636   scope->req_spf = scope->spf;
637
638   scope->bpf = scope->width * scope->height * 4;
639
640   if (scope->pixelbuf)
641     g_free (scope->pixelbuf);
642   scope->pixelbuf = g_malloc0 (scope->bpf);
643
644   if (klass->setup)
645     res = klass->setup (scope);
646
647   GST_DEBUG_OBJECT (scope, "video: dimension %dx%d, framerate %d/%d",
648       scope->width, scope->height, scope->fps_n, scope->fps_d);
649   GST_DEBUG_OBJECT (scope, "blocks: spf %u, req_spf %u",
650       scope->spf, scope->req_spf);
651
652   res = gst_pad_push_event (scope->srcpad, gst_event_new_caps (caps));
653
654   return res;
655
656   /* ERRORS */
657 error:
658   {
659     GST_DEBUG_OBJECT (scope, "error parsing caps");
660     return FALSE;
661   }
662 }
663
664 static gboolean
665 gst_base_audio_visualizer_src_negotiate (GstBaseAudioVisualizer * scope)
666 {
667   GstCaps *othercaps, *target;
668   GstStructure *structure;
669   GstCaps *templ;
670   GstQuery *query;
671   GstBufferPool *pool;
672   GstStructure *config;
673   guint size, min, max;
674
675   templ = gst_pad_get_pad_template_caps (scope->srcpad);
676
677   GST_DEBUG_OBJECT (scope, "performing negotiation");
678
679   /* see what the peer can do */
680   othercaps = gst_pad_peer_query_caps (scope->srcpad, NULL);
681   if (othercaps) {
682     target = gst_caps_intersect (othercaps, templ);
683     gst_caps_unref (othercaps);
684     gst_caps_unref (templ);
685
686     if (gst_caps_is_empty (target))
687       goto no_format;
688
689     target = gst_caps_truncate (target);
690   } else {
691     target = templ;
692   }
693
694   target = gst_caps_make_writable (target);
695   structure = gst_caps_get_structure (target, 0);
696   gst_structure_fixate_field_nearest_int (structure, "width", scope->width);
697   gst_structure_fixate_field_nearest_int (structure, "height", scope->height);
698   gst_structure_fixate_field_nearest_fraction (structure, "framerate",
699       scope->fps_n, scope->fps_d);
700
701   GST_DEBUG_OBJECT (scope, "final caps are %" GST_PTR_FORMAT, target);
702
703   gst_base_audio_visualizer_src_setcaps (scope, target);
704
705   /* try to get a bufferpool now */
706   /* find a pool for the negotiated caps now */
707   query = gst_query_new_allocation (target, TRUE);
708
709   if (!gst_pad_peer_query (scope->srcpad, query)) {
710     /* not a problem, we use the query defaults */
711     GST_DEBUG_OBJECT (scope, "allocation query failed");
712   }
713
714   if (gst_query_get_n_allocation_pools (query) > 0) {
715     /* we got configuration from our peer, parse them */
716     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
717   } else {
718     pool = NULL;
719     size = scope->bpf;
720     min = max = 0;
721   }
722
723   if (pool == NULL) {
724     /* we did not get a pool, make one ourselves then */
725     pool = gst_buffer_pool_new ();
726   }
727
728   config = gst_buffer_pool_get_config (pool);
729   gst_buffer_pool_config_set_params (config, target, size, min, max);
730   gst_buffer_pool_set_config (pool, config);
731
732   if (scope->pool) {
733     gst_buffer_pool_set_active (scope->pool, FALSE);
734     gst_object_unref (scope->pool);
735   }
736   scope->pool = pool;
737
738   /* and activate */
739   gst_buffer_pool_set_active (pool, TRUE);
740
741   gst_caps_unref (target);
742
743   return TRUE;
744
745 no_format:
746   {
747     gst_caps_unref (target);
748     return FALSE;
749   }
750 }
751
752 /* make sure we are negotiated */
753 static GstFlowReturn
754 gst_base_audio_visualizer_ensure_negotiated (GstBaseAudioVisualizer * scope)
755 {
756   gboolean reconfigure;
757
758   reconfigure = gst_pad_check_reconfigure (scope->srcpad);
759
760   /* we don't know an output format yet, pick one */
761   if (reconfigure || !gst_pad_has_current_caps (scope->srcpad)) {
762     if (!gst_base_audio_visualizer_src_negotiate (scope))
763       return GST_FLOW_NOT_NEGOTIATED;
764   }
765   return GST_FLOW_OK;
766 }
767
768 static GstFlowReturn
769 gst_base_audio_visualizer_chain (GstPad * pad, GstObject * parent,
770     GstBuffer * buffer)
771 {
772   GstFlowReturn ret = GST_FLOW_OK;
773   GstBaseAudioVisualizer *scope;
774   GstBaseAudioVisualizerClass *klass;
775   GstBuffer *inbuf;
776   guint64 dist, ts;
777   guint avail, sbpf;
778   gpointer adata;
779   gboolean (*render) (GstBaseAudioVisualizer * scope, GstBuffer * audio,
780       GstBuffer * video);
781   gint bps, channels, rate;
782
783   scope = GST_BASE_AUDIO_VISUALIZER (parent);
784   klass = GST_BASE_AUDIO_VISUALIZER_CLASS (G_OBJECT_GET_CLASS (scope));
785
786   render = klass->render;
787
788   GST_LOG_OBJECT (scope, "chainfunc called");
789
790   /* resync on DISCONT */
791   if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT)) {
792     gst_adapter_clear (scope->adapter);
793   }
794
795   /* Make sure have an output format */
796   ret = gst_base_audio_visualizer_ensure_negotiated (scope);
797   if (ret != GST_FLOW_OK) {
798     gst_buffer_unref (buffer);
799     goto beach;
800   }
801   channels = GST_AUDIO_INFO_CHANNELS (&scope->ainfo);
802   rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
803   bps = GST_AUDIO_INFO_BPS (&scope->ainfo);
804
805   if (bps == 0) {
806     ret = GST_FLOW_NOT_NEGOTIATED;
807     goto beach;
808   }
809
810   gst_adapter_push (scope->adapter, buffer);
811
812   g_mutex_lock (&scope->config_lock);
813
814   /* this is what we want */
815   sbpf = scope->req_spf * channels * sizeof (gint16);
816
817   inbuf = scope->inbuf;
818   /* FIXME: the timestamp in the adapter would be different */
819   gst_buffer_copy_into (inbuf, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
820
821   /* this is what we have */
822   avail = gst_adapter_available (scope->adapter);
823   GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
824   while (avail >= sbpf) {
825     GstBuffer *outbuf;
826     GstMapInfo map;
827
828     /* get timestamp of the current adapter content */
829     ts = gst_adapter_prev_timestamp (scope->adapter, &dist);
830     if (GST_CLOCK_TIME_IS_VALID (ts)) {
831       /* convert bytes to time */
832       dist /= bps;
833       ts += gst_util_uint64_scale_int (dist, GST_SECOND, rate);
834     }
835
836     if (GST_CLOCK_TIME_IS_VALID (ts)) {
837       gint64 qostime;
838       gboolean need_skip;
839
840       qostime =
841           gst_segment_to_running_time (&scope->segment, GST_FORMAT_TIME, ts) +
842           scope->frame_duration;
843
844       GST_OBJECT_LOCK (scope);
845       /* check for QoS, don't compute buffers that are known to be late */
846       need_skip = scope->earliest_time != -1 && qostime <= scope->earliest_time;
847       GST_OBJECT_UNLOCK (scope);
848
849       if (need_skip) {
850         GST_WARNING_OBJECT (scope,
851             "QoS: skip ts: %" GST_TIME_FORMAT ", earliest: %" GST_TIME_FORMAT,
852             GST_TIME_ARGS (qostime), GST_TIME_ARGS (scope->earliest_time));
853         goto skip;
854       }
855     }
856
857     g_mutex_unlock (&scope->config_lock);
858     ret = gst_buffer_pool_acquire_buffer (scope->pool, &outbuf, NULL);
859     g_mutex_lock (&scope->config_lock);
860     /* recheck as the value could have changed */
861     sbpf = scope->req_spf * channels * sizeof (gint16);
862
863     /* no buffer allocated, we don't care why. */
864     if (ret != GST_FLOW_OK)
865       break;
866
867     /* sync controlled properties */
868     gst_object_sync_values (GST_OBJECT (scope), ts);
869
870     GST_BUFFER_TIMESTAMP (outbuf) = ts;
871     GST_BUFFER_DURATION (outbuf) = scope->frame_duration;
872
873     gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
874     if (scope->shader) {
875       memcpy (map.data, scope->pixelbuf, scope->bpf);
876     } else {
877       memset (map.data, 0, scope->bpf);
878     }
879
880     /* this can fail as the data size we need could have changed */
881     if (!(adata = (gpointer) gst_adapter_map (scope->adapter, sbpf)))
882       break;
883
884     gst_buffer_append_memory (inbuf,
885         gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, adata, sbpf, 0,
886             sbpf, NULL, NULL));
887
888     /* call class->render() vmethod */
889     if (render) {
890       if (!render (scope, inbuf, outbuf)) {
891         ret = GST_FLOW_ERROR;
892       } else {
893         /* run various post processing (shading and geometri transformation */
894         if (scope->shader) {
895           scope->shader (scope, map.data, scope->pixelbuf);
896         }
897       }
898     }
899
900     gst_buffer_unmap (outbuf, &map);
901     gst_buffer_resize (outbuf, 0, scope->bpf);
902
903     g_mutex_unlock (&scope->config_lock);
904     ret = gst_pad_push (scope->srcpad, outbuf);
905     outbuf = NULL;
906     g_mutex_lock (&scope->config_lock);
907
908   skip:
909     /* recheck as the value could have changed */
910     sbpf = scope->req_spf * channels * sizeof (gint16);
911     GST_LOG_OBJECT (scope, "avail: %u, bpf: %u", avail, sbpf);
912     /* we want to take less or more, depending on spf : req_spf */
913     if (avail - sbpf >= sbpf) {
914       gst_adapter_flush (scope->adapter, sbpf);
915       gst_adapter_unmap (scope->adapter);
916     } else if (avail >= sbpf) {
917       /* just flush a bit and stop */
918       gst_adapter_flush (scope->adapter, (avail - sbpf));
919       gst_adapter_unmap (scope->adapter);
920       break;
921     }
922     avail = gst_adapter_available (scope->adapter);
923
924     if (ret != GST_FLOW_OK)
925       break;
926   }
927
928   g_mutex_unlock (&scope->config_lock);
929
930 beach:
931   return ret;
932 }
933
934 static gboolean
935 gst_base_audio_visualizer_src_event (GstPad * pad, GstObject * parent,
936     GstEvent * event)
937 {
938   gboolean res;
939   GstBaseAudioVisualizer *scope;
940
941   scope = GST_BASE_AUDIO_VISUALIZER (parent);
942
943   switch (GST_EVENT_TYPE (event)) {
944     case GST_EVENT_QOS:
945     {
946       gdouble proportion;
947       GstClockTimeDiff diff;
948       GstClockTime timestamp;
949
950       gst_event_parse_qos (event, NULL, &proportion, &diff, &timestamp);
951
952       /* save stuff for the _chain() function */
953       GST_OBJECT_LOCK (scope);
954       scope->proportion = proportion;
955       if (diff >= 0)
956         /* we're late, this is a good estimate for next displayable
957          * frame (see part-qos.txt) */
958         scope->earliest_time = timestamp + 2 * diff + scope->frame_duration;
959       else
960         scope->earliest_time = timestamp + diff;
961       GST_OBJECT_UNLOCK (scope);
962
963       res = gst_pad_push_event (scope->sinkpad, event);
964       break;
965     }
966     default:
967       res = gst_pad_push_event (scope->sinkpad, event);
968       break;
969   }
970
971   return res;
972 }
973
974 static gboolean
975 gst_base_audio_visualizer_sink_event (GstPad * pad, GstObject * parent,
976     GstEvent * event)
977 {
978   gboolean res;
979   GstBaseAudioVisualizer *scope;
980
981   scope = GST_BASE_AUDIO_VISUALIZER (parent);
982
983   switch (GST_EVENT_TYPE (event)) {
984     case GST_EVENT_CAPS:
985     {
986       GstCaps *caps;
987
988       gst_event_parse_caps (event, &caps);
989       res = gst_base_audio_visualizer_sink_setcaps (scope, caps);
990       break;
991     }
992     case GST_EVENT_FLUSH_START:
993       res = gst_pad_push_event (scope->srcpad, event);
994       break;
995     case GST_EVENT_FLUSH_STOP:
996       gst_base_audio_visualizer_reset (scope);
997       res = gst_pad_push_event (scope->srcpad, event);
998       break;
999     case GST_EVENT_SEGMENT:
1000     {
1001       /* the newsegment values are used to clip the input samples
1002        * and to convert the incomming timestamps to running time so
1003        * we can do QoS */
1004       gst_event_copy_segment (event, &scope->segment);
1005
1006       res = gst_pad_push_event (scope->srcpad, event);
1007       break;
1008     }
1009     default:
1010       res = gst_pad_push_event (scope->srcpad, event);
1011       break;
1012   }
1013
1014   return res;
1015 }
1016
1017 static gboolean
1018 gst_base_audio_visualizer_src_query (GstPad * pad, GstObject * parent,
1019     GstQuery * query)
1020 {
1021   gboolean res = FALSE;
1022   GstBaseAudioVisualizer *scope;
1023
1024   scope = GST_BASE_AUDIO_VISUALIZER (parent);
1025
1026   switch (GST_QUERY_TYPE (query)) {
1027     case GST_QUERY_LATENCY:
1028     {
1029       /* We need to send the query upstream and add the returned latency to our
1030        * own */
1031       GstClockTime min_latency, max_latency;
1032       gboolean us_live;
1033       GstClockTime our_latency;
1034       guint max_samples;
1035       gint rate = GST_AUDIO_INFO_RATE (&scope->ainfo);
1036
1037       if (rate == 0)
1038         break;
1039
1040       if ((res = gst_pad_peer_query (scope->sinkpad, query))) {
1041         gst_query_parse_latency (query, &us_live, &min_latency, &max_latency);
1042
1043         GST_DEBUG_OBJECT (scope, "Peer latency: min %"
1044             GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1045             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1046
1047         /* the max samples we must buffer buffer */
1048         max_samples = MAX (scope->req_spf, scope->spf);
1049         our_latency = gst_util_uint64_scale_int (max_samples, GST_SECOND, rate);
1050
1051         GST_DEBUG_OBJECT (scope, "Our latency: %" GST_TIME_FORMAT,
1052             GST_TIME_ARGS (our_latency));
1053
1054         /* we add some latency but only if we need to buffer more than what
1055          * upstream gives us */
1056         min_latency += our_latency;
1057         if (max_latency != -1)
1058           max_latency += our_latency;
1059
1060         GST_DEBUG_OBJECT (scope, "Calculated total latency : min %"
1061             GST_TIME_FORMAT " max %" GST_TIME_FORMAT,
1062             GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
1063
1064         gst_query_set_latency (query, TRUE, min_latency, max_latency);
1065       }
1066       break;
1067     }
1068     default:
1069       res = gst_pad_query_default (pad, parent, query);
1070       break;
1071   }
1072
1073   return res;
1074 }
1075
1076 static gboolean
1077 gst_base_audio_visualizer_sink_query (GstPad * pad, GstObject * parent,
1078     GstQuery * query)
1079 {
1080   gboolean res = FALSE;
1081
1082   switch (GST_QUERY_TYPE (query)) {
1083     default:
1084       res = gst_pad_query_default (pad, parent, query);
1085       break;
1086   }
1087   return res;
1088 }
1089
1090 static GstStateChangeReturn
1091 gst_base_audio_visualizer_change_state (GstElement * element,
1092     GstStateChange transition)
1093 {
1094   GstStateChangeReturn ret;
1095   GstBaseAudioVisualizer *scope;
1096
1097   scope = GST_BASE_AUDIO_VISUALIZER (element);
1098
1099   switch (transition) {
1100     case GST_STATE_CHANGE_READY_TO_PAUSED:
1101       gst_base_audio_visualizer_reset (scope);
1102       break;
1103     default:
1104       break;
1105   }
1106
1107   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1108
1109   switch (transition) {
1110     case GST_STATE_CHANGE_PAUSED_TO_READY:
1111       if (scope->pool) {
1112         gst_buffer_pool_set_active (scope->pool, FALSE);
1113         gst_object_replace ((GstObject **) & scope->pool, NULL);
1114       }
1115       break;
1116     case GST_STATE_CHANGE_READY_TO_NULL:
1117       break;
1118     default:
1119       break;
1120   }
1121
1122   return ret;
1123 }