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