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