Add -Wmissing-declarations -Wmissing-prototypes warning flags
[platform/upstream/gstreamer.git] / tests / check / elements / rgvolume.c
1 /* GStreamer ReplayGain volume adjustment
2  *
3  * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
4  *
5  * rgvolume.c: Unit test for the rgvolume element
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  * 
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  * 
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #include <gst/check/gstcheck.h>
24
25 #include <math.h>
26
27 GList *buffers = NULL;
28 GList *events = NULL;
29
30 /* For ease of programming we use globals to keep refs for our floating src and
31  * sink pads we create; otherwise we always have to do get_pad, get_peer, and
32  * then remove references in every test function */
33 static GstPad *mysrcpad, *mysinkpad;
34
35 #define RG_VOLUME_CAPS_TEMPLATE_STRING    \
36   "audio/x-raw-float, "                   \
37   "width = (int) 32, "                    \
38   "endianness = (int) BYTE_ORDER, "       \
39   "channels = (int) [ 1, MAX ], "         \
40   "rate = (int) [ 1, MAX ]"
41
42 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
46     );
47 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING)
51     );
52
53 /* gstcheck sets up a chain function that appends buffers to a global list.
54  * This is our equivalent of that for event handling. */
55 static gboolean
56 event_func (GstPad * pad, GstEvent * event)
57 {
58   events = g_list_append (events, event);
59
60   return TRUE;
61 }
62
63 static GstElement *
64 setup_rgvolume (void)
65 {
66   GstElement *element;
67
68   GST_DEBUG ("setup_rgvolume");
69   element = gst_check_setup_element ("rgvolume");
70   mysrcpad = gst_check_setup_src_pad (element, &srctemplate, NULL);
71   mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate, NULL);
72
73   /* Capture events, to test tag filtering behavior: */
74   gst_pad_set_event_function (mysinkpad, event_func);
75
76   gst_pad_set_active (mysrcpad, TRUE);
77   gst_pad_set_active (mysinkpad, TRUE);
78
79   return element;
80 }
81
82 static void
83 cleanup_rgvolume (GstElement * element)
84 {
85   GST_DEBUG ("cleanup_rgvolume");
86
87   g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
88   g_list_free (buffers);
89   buffers = NULL;
90
91   g_list_foreach (events, (GFunc) gst_mini_object_unref, NULL);
92   g_list_free (events);
93   events = NULL;
94
95   gst_pad_set_active (mysrcpad, FALSE);
96   gst_pad_set_active (mysinkpad, FALSE);
97   gst_check_teardown_src_pad (element);
98   gst_check_teardown_sink_pad (element);
99   gst_check_teardown_element (element);
100 }
101
102 static void
103 set_playing_state (GstElement * element)
104 {
105   fail_unless (gst_element_set_state (element,
106           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
107       "Could not set state to PLAYING");
108 }
109
110 static void
111 set_null_state (GstElement * element)
112 {
113   fail_unless (gst_element_set_state (element,
114           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS,
115       "Could not set state to NULL");
116 }
117
118 static void
119 send_eos_event (GstElement * element)
120 {
121   GstEvent *event = gst_event_new_eos ();
122
123   fail_unless (g_list_length (events) == 0);
124   fail_unless (gst_pad_push_event (mysrcpad, event),
125       "Pushing EOS event failed");
126   fail_unless (g_list_length (events) == 1);
127   fail_unless (events->data == event);
128   gst_mini_object_unref ((GstMiniObject *) events->data);
129   events = g_list_remove (events, event);
130 }
131
132 static GstEvent *
133 send_tag_event (GstElement * element, GstEvent * event)
134 {
135   g_return_val_if_fail (event->type == GST_EVENT_TAG, NULL);
136
137   fail_unless (g_list_length (events) == 0);
138   fail_unless (gst_pad_push_event (mysrcpad, event),
139       "Pushing tag event failed");
140
141   if (g_list_length (events) == 0) {
142     /* Event got filtered out. */
143     event = NULL;
144   } else {
145     GstTagList *tag_list;
146     gdouble dummy;
147
148     event = events->data;
149     events = g_list_remove (events, event);
150
151     fail_unless (event->type == GST_EVENT_TAG);
152     gst_event_parse_tag (event, &tag_list);
153
154     /* The element is supposed to filter out ReplayGain related tags. */
155     fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &dummy),
156         "tag event still contains track gain tag");
157     fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &dummy),
158         "tag event still contains track peak tag");
159     fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &dummy),
160         "tag event still contains album gain tag");
161     fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &dummy),
162         "tag event still contains album peak tag");
163   }
164
165   return event;
166 }
167
168 static GstBuffer *
169 test_buffer_new (gfloat value)
170 {
171   GstBuffer *buf;
172   GstCaps *caps;
173   gfloat *data;
174   gint i;
175
176   buf = gst_buffer_new_and_alloc (8 * sizeof (gfloat));
177   data = (gfloat *) GST_BUFFER_DATA (buf);
178   for (i = 0; i < 8; i++)
179     data[i] = value;
180
181   caps = gst_caps_from_string ("audio/x-raw-float, "
182       "rate = 8000, channels = 1, endianness = BYTE_ORDER, width = 32");
183   gst_buffer_set_caps (buf, caps);
184   gst_caps_unref (caps);
185
186   ASSERT_BUFFER_REFCOUNT (buf, "buf", 1);
187
188   return buf;
189 }
190
191 #define MATCH_GAIN(g1, g2) ((g1 < g2 + 1e-6) && (g2 < g1 + 1e-6))
192
193 static void
194 fail_unless_target_gain (GstElement * element, gdouble expected_gain)
195 {
196   gdouble prop_gain;
197
198   g_object_get (element, "target-gain", &prop_gain, NULL);
199
200   fail_unless (MATCH_GAIN (prop_gain, expected_gain),
201       "Target gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
202 }
203
204 static void
205 fail_unless_result_gain (GstElement * element, gdouble expected_gain)
206 {
207   GstBuffer *input_buf, *output_buf;
208   gfloat input_sample, output_sample;
209   gdouble gain, prop_gain;
210   gboolean is_passthrough, expect_passthrough;
211   gint i;
212
213   fail_unless (g_list_length (buffers) == 0);
214
215   input_sample = 1.0;
216   input_buf = test_buffer_new (input_sample);
217
218   /* We keep an extra reference to detect passthrough mode. */
219   gst_buffer_ref (input_buf);
220   /* Pushing steals a reference. */
221   fail_unless (gst_pad_push (mysrcpad, input_buf) == GST_FLOW_OK);
222   gst_buffer_unref (input_buf);
223
224   /* The output buffer ends up on the global buffer list. */
225   fail_unless (g_list_length (buffers) == 1);
226   output_buf = buffers->data;
227   fail_if (output_buf == NULL);
228
229   buffers = g_list_remove (buffers, output_buf);
230   ASSERT_BUFFER_REFCOUNT (output_buf, "output_buf", 1);
231   fail_unless_equals_int (GST_BUFFER_SIZE (output_buf), 8 * sizeof (gfloat));
232
233   output_sample = *((gfloat *) GST_BUFFER_DATA (output_buf));
234
235   fail_if (output_sample == 0.0, "First output sample is zero");
236   for (i = 1; i < 8; i++) {
237     gfloat output = ((gfloat *) GST_BUFFER_DATA (output_buf))[i];
238
239     fail_unless (output_sample == output, "Output samples not uniform");
240   };
241
242   gain = 20. * log10 (output_sample / input_sample);
243   fail_unless (MATCH_GAIN (gain, expected_gain),
244       "Applied gain is %.2f dB, expected %.2f dB", gain, expected_gain);
245   g_object_get (element, "result-gain", &prop_gain, NULL);
246   fail_unless (MATCH_GAIN (prop_gain, expected_gain),
247       "Result gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain);
248
249   is_passthrough = (output_buf == input_buf);
250   expect_passthrough = MATCH_GAIN (expected_gain, +0.00);
251   fail_unless (is_passthrough == expect_passthrough,
252       expect_passthrough
253       ? "Expected operation in passthrough mode"
254       : "Incorrect passthrough behaviour");
255
256   gst_buffer_unref (output_buf);
257 }
258
259 static void
260 fail_unless_gain (GstElement * element, gdouble expected_gain)
261 {
262   fail_unless_target_gain (element, expected_gain);
263   fail_unless_result_gain (element, expected_gain);
264 }
265
266 /* Start of tests. */
267
268 GST_START_TEST (test_no_buffer)
269 {
270   GstElement *element = setup_rgvolume ();
271
272   set_playing_state (element);
273   set_null_state (element);
274   set_playing_state (element);
275   send_eos_event (element);
276
277   cleanup_rgvolume (element);
278 }
279
280 GST_END_TEST;
281
282 GST_START_TEST (test_events)
283 {
284   GstElement *element = setup_rgvolume ();
285   GstEvent *event;
286   GstEvent *new_event;
287   GstTagList *tag_list;
288   gchar *artist;
289
290   set_playing_state (element);
291
292   tag_list = gst_tag_list_new ();
293   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
294       GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
295       GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
296       GST_TAG_ARTIST, "Foobar", NULL);
297   event = gst_event_new_tag (tag_list);
298   new_event = send_tag_event (element, event);
299   /* Expect the element to modify the writable event. */
300   fail_unless (event == new_event, "Writable tag event not reused");
301   gst_event_parse_tag (new_event, &tag_list);
302   fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
303   fail_unless (g_str_equal (artist, "Foobar"));
304   g_free (artist);
305   gst_event_unref (new_event);
306
307   /* Same as above, but with a non-writable event. */
308
309   tag_list = gst_tag_list_new ();
310   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
311       GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463,
312       GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415,
313       GST_TAG_ARTIST, "Foobar", NULL);
314   event = gst_event_new_tag (tag_list);
315   /* Holding an extra ref makes the event unwritable: */
316   gst_event_ref (event);
317   new_event = send_tag_event (element, event);
318   fail_unless (event != new_event, "Unwritable tag event reused");
319   gst_event_parse_tag (new_event, &tag_list);
320   fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist));
321   fail_unless (g_str_equal (artist, "Foobar"));
322   g_free (artist);
323   gst_event_unref (event);
324   gst_event_unref (new_event);
325
326   cleanup_rgvolume (element);
327 }
328
329 GST_END_TEST;
330
331 GST_START_TEST (test_simple)
332 {
333   GstElement *element = setup_rgvolume ();
334   GstTagList *tag_list;
335
336   g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
337       "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
338   set_playing_state (element);
339
340   tag_list = gst_tag_list_new ();
341   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
342       GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
343       GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
344   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
345   fail_unless_gain (element, -9.45);    /* pre-amp + track gain */
346   send_eos_event (element);
347
348   g_object_set (element, "album-mode", TRUE, NULL);
349
350   tag_list = gst_tag_list_new ();
351   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
352       GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0,
353       GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL);
354   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
355   fail_unless_gain (element, -3.91);    /* pre-amp + album gain */
356
357   /* Switching back to track mode in the middle of a stream: */
358   g_object_set (element, "album-mode", FALSE, NULL);
359   fail_unless_gain (element, -9.45);    /* pre-amp + track gain */
360   send_eos_event (element);
361
362   cleanup_rgvolume (element);
363 }
364
365 GST_END_TEST;
366
367 /* If there are no gain tags at all, the fallback gain is used. */
368
369 GST_START_TEST (test_fallback_gain)
370 {
371   GstElement *element = setup_rgvolume ();
372   GstTagList *tag_list;
373
374   /* First some track where fallback does _not_ apply. */
375
376   g_object_set (element, "album-mode", FALSE, "headroom", 10.00,
377       "pre-amp", -6.00, "fallback-gain", -3.00, NULL);
378   set_playing_state (element);
379
380   tag_list = gst_tag_list_new ();
381   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
382       GST_TAG_TRACK_GAIN, +3.5, GST_TAG_TRACK_PEAK, 1.0,
383       GST_TAG_ALBUM_GAIN, -0.5, GST_TAG_ALBUM_PEAK, 1.0, NULL);
384   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
385   fail_unless_gain (element, -2.50);    /* pre-amp + track gain */
386   send_eos_event (element);
387
388   /* Now a track completely missing tags. */
389
390   fail_unless_gain (element, -9.00);    /* pre-amp + fallback-gain */
391
392   /* Changing the fallback gain in the middle of a stream, going to pass-through
393    * mode: */
394   g_object_set (element, "fallback-gain", +6.00, NULL);
395   fail_unless_gain (element, +0.00);    /* pre-amp + fallback-gain */
396   send_eos_event (element);
397
398   /* Verify that result gain is set to +0.00 with pre-amp + fallback-gain >
399    * +0.00 and no headroom. */
400
401   g_object_set (element, "fallback-gain", +12.00, "headroom", +0.00, NULL);
402   fail_unless_target_gain (element, +6.00);     /* pre-amp + fallback-gain */
403   fail_unless_result_gain (element, +0.00);
404   send_eos_event (element);
405
406   cleanup_rgvolume (element);
407 }
408
409 GST_END_TEST;
410
411 /* If album gain is to be preferred but not available, the track gain is to be
412  * taken instead. */
413
414 GST_START_TEST (test_fallback_track)
415 {
416   GstElement *element = setup_rgvolume ();
417   GstTagList *tag_list;
418
419   g_object_set (element, "album-mode", TRUE, "headroom", +0.00,
420       "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
421   set_playing_state (element);
422
423   tag_list = gst_tag_list_new ();
424   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
425       GST_TAG_TRACK_GAIN, +2.11, GST_TAG_TRACK_PEAK, 1.0, NULL);
426   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
427   fail_unless_gain (element, -3.89);    /* pre-amp + track gain */
428
429   send_eos_event (element);
430
431   cleanup_rgvolume (element);
432 }
433
434 GST_END_TEST;
435
436 /* If track gain is to be preferred but not available, the album gain is to be
437  * taken instead. */
438
439 GST_START_TEST (test_fallback_album)
440 {
441   GstElement *element = setup_rgvolume ();
442   GstTagList *tag_list;
443
444   g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
445       "pre-amp", -6.00, "fallback-gain", +1.23, NULL);
446   set_playing_state (element);
447
448   tag_list = gst_tag_list_new ();
449   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
450       GST_TAG_ALBUM_GAIN, +3.73, GST_TAG_ALBUM_PEAK, 1.0, NULL);
451   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
452   fail_unless_gain (element, -2.27);    /* pre-amp + album gain */
453
454   send_eos_event (element);
455
456   cleanup_rgvolume (element);
457 }
458
459 GST_END_TEST;
460
461 GST_START_TEST (test_headroom)
462 {
463   GstElement *element = setup_rgvolume ();
464   GstTagList *tag_list;
465
466   g_object_set (element, "album-mode", FALSE, "headroom", +0.00,
467       "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
468   set_playing_state (element);
469
470   tag_list = gst_tag_list_new ();
471   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
472       GST_TAG_TRACK_GAIN, +3.50, GST_TAG_TRACK_PEAK, 1.0, NULL);
473   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
474   fail_unless_target_gain (element, +3.50);     /* pre-amp + track gain */
475   fail_unless_result_gain (element, +0.00);
476   send_eos_event (element);
477
478   g_object_set (element, "headroom", +2.00, NULL);
479   tag_list = gst_tag_list_new ();
480   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
481       GST_TAG_TRACK_GAIN, +9.18, GST_TAG_TRACK_PEAK, 0.687149, NULL);
482   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
483   fail_unless_target_gain (element, +9.18);     /* pre-amp + track gain */
484   /* Result is 20. * log10 (1. / peak) + headroom. */
485   fail_unless_result_gain (element, 5.2589816238303335);
486   send_eos_event (element);
487
488   g_object_set (element, "album-mode", TRUE, NULL);
489   tag_list = gst_tag_list_new ();
490   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
491       GST_TAG_ALBUM_GAIN, +5.50, GST_TAG_ALBUM_PEAK, 1.0, NULL);
492   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
493   fail_unless_target_gain (element, +5.50);     /* pre-amp + album gain */
494   fail_unless_result_gain (element, +2.00);     /* headroom */
495   send_eos_event (element);
496
497   cleanup_rgvolume (element);
498 }
499
500 GST_END_TEST;
501
502 GST_START_TEST (test_reference_level)
503 {
504   GstElement *element = setup_rgvolume ();
505   GstTagList *tag_list;
506
507   g_object_set (element,
508       "album-mode", FALSE,
509       "headroom", +0.00, "pre-amp", +0.00, "fallback-gain", +1.23, NULL);
510   set_playing_state (element);
511
512   tag_list = gst_tag_list_new ();
513   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
514       GST_TAG_TRACK_GAIN, 0.00, GST_TAG_TRACK_PEAK, 0.2,
515       GST_TAG_REFERENCE_LEVEL, 83., NULL);
516   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
517   /* Because our authorative reference is 89 dB, we bump it up by +6 dB. */
518   fail_unless_gain (element, +6.00);    /* pre-amp + track gain */
519   send_eos_event (element);
520
521   g_object_set (element, "album-mode", TRUE, NULL);
522
523   /* Same as above, but with album gain. */
524
525   tag_list = gst_tag_list_new ();
526   gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE,
527       GST_TAG_TRACK_GAIN, 1.23, GST_TAG_TRACK_PEAK, 0.1,
528       GST_TAG_ALBUM_GAIN, 0.00, GST_TAG_ALBUM_PEAK, 0.2,
529       GST_TAG_REFERENCE_LEVEL, 83., NULL);
530   fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL);
531   fail_unless_gain (element, +6.00);    /* pre-amp + album gain */
532
533   cleanup_rgvolume (element);
534 }
535
536 GST_END_TEST;
537
538 static Suite *
539 rgvolume_suite (void)
540 {
541   Suite *s = suite_create ("rgvolume");
542   TCase *tc_chain = tcase_create ("general");
543
544   suite_add_tcase (s, tc_chain);
545
546   tcase_add_test (tc_chain, test_no_buffer);
547   tcase_add_test (tc_chain, test_events);
548   tcase_add_test (tc_chain, test_simple);
549   tcase_add_test (tc_chain, test_fallback_gain);
550   tcase_add_test (tc_chain, test_fallback_track);
551   tcase_add_test (tc_chain, test_fallback_album);
552   tcase_add_test (tc_chain, test_headroom);
553   tcase_add_test (tc_chain, test_reference_level);
554
555   return s;
556 }
557
558 int
559 main (int argc, char **argv)
560 {
561   gint nf;
562
563   Suite *s = rgvolume_suite ();
564   SRunner *sr = srunner_create (s);
565
566   gst_check_init (&argc, &argv);
567
568   srunner_run_all (sr, CK_ENV);
569   nf = srunner_ntests_failed (sr);
570   srunner_free (sr);
571
572   return nf;
573 }