diff options
author | René Stadler <mail@renestadler.de> | 2007-05-19 10:01:45 +0000 |
---|---|---|
committer | Tim-Philipp Müller <tim@centricular.net> | 2007-05-19 10:01:45 +0000 |
commit | 1645110f020d26eaa9700775dbedf646918fdd23 (patch) | |
tree | e58b53d9e4dd90f1e2b129bee0851a9695258950 | |
parent | 8d971fb7fab676c42e2bc64f4f0a5e633591ec72 (diff) | |
download | gst-plugins-bad-1645110f020d26eaa9700775dbedf646918fdd23.tar.gz gst-plugins-bad-1645110f020d26eaa9700775dbedf646918fdd23.tar.bz2 gst-plugins-bad-1645110f020d26eaa9700775dbedf646918fdd23.zip |
Add replaygain playback elements (#412710).
Original commit message from CVS:
Patch by: René Stadler <mail at renestadler de>
* docs/plugins/Makefile.am:
* docs/plugins/gst-plugins-bad-plugins-docs.sgml:
* docs/plugins/gst-plugins-bad-plugins-sections.txt:
* docs/plugins/inspect/plugin-replaygain.xml:
* gst/replaygain/Makefile.am:
* gst/replaygain/gstrganalysis.c: (gst_rg_analysis_class_init),
(gst_rg_analysis_start), (gst_rg_analysis_set_caps),
(gst_rg_analysis_transform_ip), (gst_rg_analysis_event),
(gst_rg_analysis_stop), (gst_rg_analysis_handle_tags),
(gst_rg_analysis_handle_eos), (gst_rg_analysis_track_result),
(gst_rg_analysis_album_result):
* gst/replaygain/gstrganalysis.h:
* gst/replaygain/gstrglimiter.c: (gst_rg_limiter_base_init),
(gst_rg_limiter_class_init), (gst_rg_limiter_init),
(gst_rg_limiter_set_property), (gst_rg_limiter_get_property),
(gst_rg_limiter_transform_ip):
* gst/replaygain/gstrglimiter.h:
* gst/replaygain/gstrgvolume.c: (gst_rg_volume_base_init),
(gst_rg_volume_class_init), (gst_rg_volume_init),
(gst_rg_volume_set_property), (gst_rg_volume_get_property),
(gst_rg_volume_dispose), (gst_rg_volume_change_state),
(gst_rg_volume_sink_event), (gst_rg_volume_tag_event),
(gst_rg_volume_reset), (gst_rg_volume_update_gain),
(gst_rg_volume_determine_gain):
* gst/replaygain/gstrgvolume.h:
* gst/replaygain/replaygain.c: (plugin_init):
* gst/replaygain/replaygain.h:
* gst/replaygain/rganalysis.h:
* tests/check/Makefile.am:
* tests/check/elements/.cvsignore:
* tests/check/elements/rganalysis.c: (send_eos_event),
(GST_START_TEST):
* tests/check/elements/rglimiter.c: (setup_rglimiter),
(cleanup_rglimiter), (set_playing_state), (create_test_buffer),
(verify_test_buffer), (GST_START_TEST), (rglimiter_suite), (main):
* tests/check/elements/rgvolume.c: (event_func), (setup_rgvolume),
(cleanup_rgvolume), (set_playing_state), (set_null_state),
(send_eos_event), (send_tag_event), (test_buffer_new),
(fail_unless_target_gain), (fail_unless_result_gain),
(fail_unless_gain), (GST_START_TEST), (rgvolume_suite), (main):
Add replaygain playback elements (#412710).
-rw-r--r-- | ChangeLog | 46 | ||||
-rw-r--r-- | docs/plugins/Makefile.am | 2 | ||||
-rw-r--r-- | docs/plugins/gst-plugins-bad-plugins-docs.sgml | 2 | ||||
-rw-r--r-- | docs/plugins/gst-plugins-bad-plugins-sections.txt | 16 | ||||
-rw-r--r-- | docs/plugins/inspect/plugin-replaygain.xml | 16 | ||||
-rw-r--r-- | gst/replaygain/Makefile.am | 12 | ||||
-rw-r--r-- | gst/replaygain/gstrganalysis.c | 298 | ||||
-rw-r--r-- | gst/replaygain/gstrganalysis.h | 2 | ||||
-rw-r--r-- | gst/replaygain/gstrglimiter.c | 197 | ||||
-rw-r--r-- | gst/replaygain/gstrglimiter.h | 64 | ||||
-rw-r--r-- | gst/replaygain/gstrgvolume.c | 702 | ||||
-rw-r--r-- | gst/replaygain/gstrgvolume.h | 88 | ||||
-rw-r--r-- | gst/replaygain/replaygain.c | 53 | ||||
-rw-r--r-- | gst/replaygain/replaygain.h | 36 | ||||
-rw-r--r-- | gst/replaygain/rganalysis.h | 2 | ||||
-rw-r--r-- | tests/check/Makefile.am | 6 | ||||
-rw-r--r-- | tests/check/elements/.gitignore | 4 | ||||
-rw-r--r-- | tests/check/elements/rganalysis.c | 187 | ||||
-rw-r--r-- | tests/check/elements/rglimiter.c | 238 | ||||
-rw-r--r-- | tests/check/elements/rgvolume.c | 573 |
20 files changed, 2302 insertions, 242 deletions
@@ -1,3 +1,49 @@ +2007-05-19 Tim-Philipp Müller <tim at centricular dot net> + + Patch by: René Stadler <mail at renestadler de> + + * docs/plugins/Makefile.am: + * docs/plugins/gst-plugins-bad-plugins-docs.sgml: + * docs/plugins/gst-plugins-bad-plugins-sections.txt: + * docs/plugins/inspect/plugin-replaygain.xml: + * gst/replaygain/Makefile.am: + * gst/replaygain/gstrganalysis.c: (gst_rg_analysis_class_init), + (gst_rg_analysis_start), (gst_rg_analysis_set_caps), + (gst_rg_analysis_transform_ip), (gst_rg_analysis_event), + (gst_rg_analysis_stop), (gst_rg_analysis_handle_tags), + (gst_rg_analysis_handle_eos), (gst_rg_analysis_track_result), + (gst_rg_analysis_album_result): + * gst/replaygain/gstrganalysis.h: + * gst/replaygain/gstrglimiter.c: (gst_rg_limiter_base_init), + (gst_rg_limiter_class_init), (gst_rg_limiter_init), + (gst_rg_limiter_set_property), (gst_rg_limiter_get_property), + (gst_rg_limiter_transform_ip): + * gst/replaygain/gstrglimiter.h: + * gst/replaygain/gstrgvolume.c: (gst_rg_volume_base_init), + (gst_rg_volume_class_init), (gst_rg_volume_init), + (gst_rg_volume_set_property), (gst_rg_volume_get_property), + (gst_rg_volume_dispose), (gst_rg_volume_change_state), + (gst_rg_volume_sink_event), (gst_rg_volume_tag_event), + (gst_rg_volume_reset), (gst_rg_volume_update_gain), + (gst_rg_volume_determine_gain): + * gst/replaygain/gstrgvolume.h: + * gst/replaygain/replaygain.c: (plugin_init): + * gst/replaygain/replaygain.h: + * gst/replaygain/rganalysis.h: + * tests/check/Makefile.am: + * tests/check/elements/.cvsignore: + * tests/check/elements/rganalysis.c: (send_eos_event), + (GST_START_TEST): + * tests/check/elements/rglimiter.c: (setup_rglimiter), + (cleanup_rglimiter), (set_playing_state), (create_test_buffer), + (verify_test_buffer), (GST_START_TEST), (rglimiter_suite), (main): + * tests/check/elements/rgvolume.c: (event_func), (setup_rgvolume), + (cleanup_rgvolume), (set_playing_state), (set_null_state), + (send_eos_event), (send_tag_event), (test_buffer_new), + (fail_unless_target_gain), (fail_unless_result_gain), + (fail_unless_gain), (GST_START_TEST), (rgvolume_suite), (main): + Add replaygain playback elements (#412710). + 2007-05-18 Jan Schmidt <thaytan@mad.scientist.com> * sys/glsink/glimagesink.c: (gst_glimage_sink_init_display): diff --git a/docs/plugins/Makefile.am b/docs/plugins/Makefile.am index 68e2c23e..2c150ede 100644 --- a/docs/plugins/Makefile.am +++ b/docs/plugins/Makefile.am @@ -99,6 +99,8 @@ EXTRA_HFILES = \ $(top_srcdir)/ext/wavpack/gstwavpackparse.h \ $(top_srcdir)/gst/qtdemux/qtdemux.h \ $(top_srcdir)/gst/replaygain/gstrganalysis.h \ + $(top_srcdir)/gst/replaygain/gstrglimiter.h \ + $(top_srcdir)/gst/replaygain/gstrgvolume.h \ $(top_srcdir)/gst/videocrop/gstvideocrop.h # Images to copy into HTML directory. diff --git a/docs/plugins/gst-plugins-bad-plugins-docs.sgml b/docs/plugins/gst-plugins-bad-plugins-docs.sgml index ee1e26bb..2e0f06b3 100644 --- a/docs/plugins/gst-plugins-bad-plugins-docs.sgml +++ b/docs/plugins/gst-plugins-bad-plugins-docs.sgml @@ -20,6 +20,8 @@ <xi:include href="xml/element-qtdemux.xml" /> <xi:include href="xml/element-osxvideosink.xml" /> <xi:include href="xml/element-rganalysis.xml" /> + <xi:include href="xml/element-rglimiter.xml" /> + <xi:include href="xml/element-rgvolume.xml" /> <xi:include href="xml/element-sdlaudiosink.xml" /> <xi:include href="xml/element-sdlvideosink.xml" /> <xi:include href="xml/element-trm.xml" /> diff --git a/docs/plugins/gst-plugins-bad-plugins-sections.txt b/docs/plugins/gst-plugins-bad-plugins-sections.txt index 4a0c33e3..8387d060 100644 --- a/docs/plugins/gst-plugins-bad-plugins-sections.txt +++ b/docs/plugins/gst-plugins-bad-plugins-sections.txt @@ -56,6 +56,22 @@ GstRgAnalysisClass </SECTION> <SECTION> +<FILE>element-rglimiter</FILE> +GstRgLimiter +<TITLE>rglimiter</TITLE> +<SUBSECTION Standard> +GstRgLimiterClass +</SECTION> + +<SECTION> +<FILE>element-rgvolume</FILE> +GstRgVolume +<TITLE>rgvolume</TITLE> +<SUBSECTION Standard> +GstRgVolumeClass +</SECTION> + +<SECTION> <FILE>element-sdlaudiosink</FILE> GstSDLAudioSink <TITLE>sdlaudiosink</TITLE> diff --git a/docs/plugins/inspect/plugin-replaygain.xml b/docs/plugins/inspect/plugin-replaygain.xml index 7c22964b..cb397292 100644 --- a/docs/plugins/inspect/plugin-replaygain.xml +++ b/docs/plugins/inspect/plugin-replaygain.xml @@ -1,6 +1,6 @@ <plugin> <name>replaygain</name> - <description>ReplayGain analysis</description> + <description>ReplayGain volume normalization</description> <filename>../../gst/replaygain/.libs/libgstreplaygain.so</filename> <basename>libgstreplaygain.so</basename> <version>0.10.4.1</version> @@ -16,5 +16,19 @@ <description>Perform the ReplayGain analysis</description> <author>René Stadler <mail@renestadler.de></author> </element> + <element> + <name>rglimiter</name> + <longname>ReplayGain limiter</longname> + <class>Filter/Effect/Audio</class> + <description>Apply signal compression to raw audio data</description> + <author>René Stadler <mail@renestadler.de></author> + </element> + <element> + <name>rgvolume</name> + <longname>ReplayGain volume</longname> + <class>Filter/Effect/Audio</class> + <description>Apply ReplayGain volume adjustment</description> + <author>René Stadler <mail@renestadler.de></author> + </element> </elements> </plugin>
\ No newline at end of file diff --git a/gst/replaygain/Makefile.am b/gst/replaygain/Makefile.am index d4523654..a0a3ca5a 100644 --- a/gst/replaygain/Makefile.am +++ b/gst/replaygain/Makefile.am @@ -2,12 +2,20 @@ plugin_LTLIBRARIES = libgstreplaygain.la libgstreplaygain_la_SOURCES = \ gstrganalysis.c \ + gstrglimiter.c \ + gstrgvolume.c \ + replaygain.c \ rganalysis.c -libgstreplaygain_la_CFLAGS = $(GST_CFLAGS) $(GST_BASE_CFLAGS) -libgstreplaygain_la_LIBADD = $(GST_LIBS) $(GST_BASE_LIBS) $(LIBM) +libgstreplaygain_la_CFLAGS = \ + $(GST_CFLAGS) $(GST_BASE_CFLAGS) $(GST_PLUGINS_BASE_CFLAGS) +libgstreplaygain_la_LIBADD = \ + $(GST_LIBS) $(GST_BASE_LIBS) $(GST_PLUGINS_BASE_LIBS) -lgstpbutils-0.10 $(LIBM) libgstreplaygain_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) # headers we need but don't want installed noinst_HEADERS = \ gstrganalysis.h \ + gstrglimiter.h \ + gstrgvolume.h \ + replaygain.h \ rganalysis.h diff --git a/gst/replaygain/gstrganalysis.c b/gst/replaygain/gstrganalysis.c index 9ad50e0d..24367786 100644 --- a/gst/replaygain/gstrganalysis.c +++ b/gst/replaygain/gstrganalysis.c @@ -22,103 +22,29 @@ /** * SECTION:element-rganalysis + * @see_also: <link linkend="GstRgVolume">rgvolume</link> * * <refsect2> * <para> - * GstRgAnalysis analyzes raw audio sample data in accordance with the - * proposed <ulink url="http://replaygain.org">ReplayGain - * standard</ulink> for calculating the ideal replay gain for music - * tracks and albums. The element is designed as a pass-through - * filter that never modifies any data. As it receives an EOS event, - * it finalizes the ongoing analysis and generates a tag list - * containing the results. It is sent downstream with a TAG event and - * posted on the message bus with a TAG message. The EOS event is - * forwarded as normal afterwards. Result tag lists at least contain - * the tags #GST_TAG_TRACK_GAIN and #GST_TAG_TRACK_PEAK. + * This element analyzes raw audio sample data in accordance with the proposed + * <ulink url="http://replaygain.org">ReplayGain standard</ulink> for + * calculating the ideal replay gain for music tracks and albums. The element + * is designed as a pass-through filter that never modifies any data. As it + * receives an EOS event, it finalizes the ongoing analysis and generates a tag + * list containing the results. It is sent downstream with a tag event and + * posted on the message bus with a tag message. The EOS event is forwarded as + * normal afterwards. Result tag lists at least contain the tags + * #GST_TAG_TRACK_GAIN, #GST_TAG_TRACK_PEAK and #GST_TAG_REFERENCE_LEVEL. * </para> - * <title>Album processing</title> * <para> - * Analyzing several streams sequentially and assigning them a common - * result gain is known as "album processing". If this gain is used - * during playback (by switching to "album mode"), all tracks receive - * the same amplification. This keeps the relative volume levels - * between the tracks intact. To enable this, set the <link - * linkend="GstRgAnalysis--num-tracks">num-tracks</link> property to - * the number of streams that will be processed as album tracks. - * Every time an EOS event is received, the value of this property - * will be decremented by one. As it reaches zero, it is assumed that - * the last track of the album finished. The tag list for the final - * stream will contain the additional tags #GST_TAG_ALBUM_GAIN and - * #GST_TAG_ALBUM_PEAK. All other streams just get the two track tags - * posted because the values for the album tags are not known before - * all tracks are analyzed. Applications need to make sure that the - * album gain and peak values are also associated with the other - * tracks when storing the results. It is thus a bit more complex to - * implement, but should not be avoided since the album gain is - * generally more valuable for use during playback than the track - * gain. - * </para> - * <title>Skipping processing</title> - * <para> - * For assisting transcoder/converter applications, the element can - * silently skip the processing of streams that already contain the - * necessary meta data tags. Data will flow as usual but the element - * will not consume CPU time and will not generate result tags. To - * enable possible skipping, set the <link - * linkend="GstRgAnalysis--forced">forced</link> property to #FALSE. - * If used in conjunction with album processing, the element will skip - * the number of remaining album tracks if a full set of tags is found - * for the first track. If a subsequent track of the album is missing - * tags, processing cannot start again. If this is undesired, your - * application has to scan all files beforehand and enable forcing of - * processing if needed. - * </para> - * <title>Tips</title> - * <itemizedlist> - * <listitem><para> - * Because the generated metadata tags become available at the end of - * streams, downstream muxer and encoder elements are normally unable - * to save them in their output since they generally save metadata in - * the file header. Therefore, it is often necessary that - * applications read the results in a bus event handler for the tag - * message. Obtaining the values this way is always needed for album - * processing since the album gain and peak values need to be - * associated with all tracks of an album, not just the last one. - * </para></listitem> - * <listitem><para> - * To perform album processing, the element has to preserve data - * between streams. This cannot survive a state change to the NULL or - * READY state. If you change your pipeline's state to NULL or READY - * between tracks, lock the rganalysis element's state using - * gst_element_set_locked_state() when it is in PAUSED or PLAYING. As - * with any other element, don't forget to unlock it again and set it - * to the NULL state before dropping the last reference. - * </para></listitem> - * <listitem><para> - * If the total number of album tracks is unknown beforehand, set the - * num-tracks property to some large value like #G_MAXINT (or set it - * to >= 2 before each track starts). Before the last track ends, set - * the property value to 1. - * </para></listitem> - * </itemizedlist> - * <title>Compliance</title> - * <para> - * Analyzing the ReplayGain pink noise reference waveform will compute - * a result of +6.00dB instead of the expected 0.00dB because the - * default reference level is 89dB. To obtain values as lined out in - * the original proposal of ReplayGain, set the <link - * linkend="GstRgAnalysis--reference-level">reference-level</link> - * property to 83. Almost all software uses 89dB as a reference - * however, which works against the tendency of the algorithm to - * advise to drastically lower the volume of music with a highly - * compressed dynamic range and high average output levels. This - * tendency is normally to be fought during playback (if wanted), by - * using a default pre-amp value of at least +6.00dB. At one point, - * the majority of analyzer implementations switched to 89dB which - * moved this adjustment to the analyzing/metadata writing process. - * This change has been acknowledged by the author of the ReplayGain - * proposal, however at the time of this writing, the webpage is still - * not updated. + * Because the generated metadata tags become available at the end of streams, + * downstream muxer and encoder elements are normally unable to save them in + * their output since they generally save metadata in the file header. + * Therefore, it is often necessary that applications read the results in a bus + * event handler for the tag message. Obtaining the values this way is always + * needed for <link linkend="GstRgAnalysis--num-tracks">album processing</link> + * since the album gain and peak values need to be associated with all tracks of + * an album, not just the last one. * </para> * <title>Example launch lines</title> * <para>Analyze a simple test waveform:</para> @@ -127,18 +53,26 @@ * </programlisting> * <para>Analyze a given file:</para> * <programlisting> - * gst-launch -t filesrc location="Some file.ogg" ! decodebin ! audioconvert ! audioresample ! rganalysis ! fakesink + * gst-launch -t filesrc location="Some file.ogg" ! decodebin \ + * ! audioconvert ! audioresample ! rganalysis ! fakesink * </programlisting> * <para>Analyze the pink noise reference file:</para> * <programlisting> - * gst-launch -t gnomevfssrc location=http://replaygain.hydrogenaudio.org/ref_pink.wav ! wavparse ! rganalysis ! fakesink + * gst-launch -t gnomevfssrc location=http://replaygain.hydrogenaudio.org/ref_pink.wav \ + * ! wavparse ! rganalysis ! fakesink * </programlisting> + * <para> + * The above launch line yields a result gain of +6 dB (instead of the expected + * +0 dB). This is not in error, refer to the <link + * linkend="GstRgAnalysis--reference-level">reference-level</link> property + * documentation for more information. + * </para> * <title>Acknowledgements</title> * <para> * This element is based on code used in the <ulink - * url="http://sjeng.org/vorbisgain.html">vorbisgain</ulink> program - * and many others. The relevant parts are copyrighted by David - * Robinson, Glen Sawyer and Frank Klemm. + * url="http://sjeng.org/vorbisgain.html">vorbisgain</ulink> program and many + * others. The relevant parts are copyrighted by David Robinson, Glen Sawyer + * and Frank Klemm. * </para> * </refsect2> */ @@ -147,11 +81,11 @@ #include <config.h> #endif -#include <string.h> #include <gst/gst.h> #include <gst/base/gstbasetransform.h> #include "gstrganalysis.h" +#include "replaygain.h" GST_DEBUG_CATEGORY_STATIC (gst_rg_analysis_debug); #define GST_CAT_DEFAULT gst_rg_analysis_debug @@ -254,18 +188,93 @@ gst_rg_analysis_class_init (GstRgAnalysisClass * klass) gobject_class->set_property = gst_rg_analysis_set_property; gobject_class->get_property = gst_rg_analysis_get_property; + /** + * GstRgAnalysis:num-tracks: + * + * Number of remaining album tracks. + * + * Analyzing several streams sequentially and assigning them a common result + * gain is known as "album processing". If this gain is used during playback + * (by switching to "album mode"), all tracks of an album receive the same + * amplification. This keeps the relative volume levels between the tracks + * intact. To enable this, set this property to the number of streams that + * will be processed as album tracks. + * + * Every time an EOS event is received, the value of this property is + * decremented by one. As it reaches zero, it is assumed that the last track + * of the album finished. The tag list for the final stream will contain the + * additional tags #GST_TAG_ALBUM_GAIN and #GST_TAG_ALBUM_PEAK. All other + * streams just get the two track tags posted because the values for the album + * tags are not known before all tracks are analyzed. Applications need to + * ensure that the album gain and peak values are also associated with the + * other tracks when storing the results. + * + * If the total number of album tracks is unknown beforehand, just ensure that + * the value is greater than 1 before each track starts. Then before the end + * of the last track, set it to the value 1. + * + * To perform album processing, the element has to preserve data between + * streams. This cannot survive a state change to the NULL or READY state. + * If you change your pipeline's state to NULL or READY between tracks, lock + * the element's state using gst_element_set_locked_state() when it is in + * PAUSED or PLAYING. + */ g_object_class_install_property (gobject_class, PROP_NUM_TRACKS, g_param_spec_int ("num-tracks", "Number of album tracks", - "Number of remaining tracks in the album", - 0, G_MAXINT, 0, G_PARAM_READWRITE)); + "Number of remaining album tracks", 0, G_MAXINT, 0, + G_PARAM_READWRITE)); + /** + * GstRgAnalysis:forced: + * + * Whether to analyze streams even when ReplayGain tags exist. + * + * For assisting transcoder/converter applications, the element can silently + * skip the processing of streams that already contain the necessary tags. + * Data will flow as usual but the element will not consume CPU time and will + * not generate result tags. To enable possible skipping, set this property + * to #FALSE. + * + * If used in conjunction with <link linkend="GstRgAnalysis--num-tracks">album + * processing</link>, the element will skip the number of remaining album + * tracks if a full set of tags is found for the first track. If a subsequent + * track of the album is missing tags, processing cannot start again. If this + * is undesired, the application has to scan all files beforehand and enable + * forcing of processing if needed. + */ g_object_class_install_property (gobject_class, PROP_FORCED, - g_param_spec_boolean ("forced", "Force processing", - "Analyze streams even when ReplayGain tags exist", + g_param_spec_boolean ("forced", "Forced", + "Analyze even if ReplayGain tags exist", FORCED_DEFAULT, G_PARAM_READWRITE)); + /** + * GstRgAnalysis:reference-level: + * + * Reference level [dB]. + * + * Analyzing the ReplayGain pink noise reference waveform computes a result of + * +6 dB instead of the expected 0 dB. This is because the default reference + * level is 89 dB. To obtain values as lined out in the original proposal of + * ReplayGain, set this property to 83. + * + * Almost all software uses 89 dB as a reference however, and this value has + * become the new official value. That is to say, while the change has been + * acclaimed by the author of the ReplayGain proposal, the <ulink + * url="http://replaygain.org">webpage</ulink> is still outdated at the time + * of this writing. + * + * The value was changed because the original proposal recommends a default + * pre-amp value of +6 dB for playback. This seemed a bit odd, as it means + * that the algorithm has the general tendency to produce adjustment values + * that are 6 dB too low. Bumping the reference level by 6 dB compensated for + * this. + * + * The problem of the reference level being ambiguous for lack of concise + * standardization is to be solved by adopting the #GST_TAG_REFERENCE_LEVEL + * tag, which allows to store the used value alongside the gain values. + */ g_object_class_install_property (gobject_class, PROP_REFERENCE_LEVEL, g_param_spec_double ("reference-level", "Reference level", - "Reference level in dB (83.0 for original proposal)", - 0.0, G_MAXDOUBLE, RG_REFERENCE_LEVEL, G_PARAM_READWRITE)); + "Reference level [dB]", 0.0, 150., RG_REFERENCE_LEVEL, + G_PARAM_READWRITE)); trans_class = (GstBaseTransformClass *) klass; trans_class->start = GST_DEBUG_FUNCPTR (gst_rg_analysis_start); @@ -346,7 +355,7 @@ gst_rg_analysis_start (GstBaseTransform * base) filter->ctx = rg_analysis_new (); filter->analyze = NULL; - GST_DEBUG_OBJECT (filter, "Started"); + GST_LOG_OBJECT (filter, "started"); return TRUE; } @@ -357,7 +366,7 @@ gst_rg_analysis_set_caps (GstBaseTransform * base, GstCaps * in_caps, { GstRgAnalysis *filter = GST_RG_ANALYSIS (base); GstStructure *structure; - const gchar *mime_type; + const gchar *name; gint n_channels, sample_rate, sample_bit_size, sample_size; g_return_val_if_fail (filter->ctx != NULL, FALSE); @@ -367,7 +376,7 @@ gst_rg_analysis_set_caps (GstBaseTransform * base, GstCaps * in_caps, in_caps, out_caps); structure = gst_caps_get_structure (in_caps, 0); - mime_type = gst_structure_get_name (structure); + name = gst_structure_get_name (structure); if (!gst_structure_get_int (structure, "width", &sample_bit_size) || !gst_structure_get_int (structure, "channels", &n_channels) @@ -381,7 +390,7 @@ gst_rg_analysis_set_caps (GstBaseTransform * base, GstCaps * in_caps, goto invalid_format; sample_size = sample_bit_size / 8; - if (strcmp (mime_type, "audio/x-raw-float") == 0) { + if (g_str_equal (name, "audio/x-raw-float")) { if (sample_size != sizeof (gfloat)) goto invalid_format; @@ -398,7 +407,7 @@ gst_rg_analysis_set_caps (GstBaseTransform * base, GstCaps * in_caps, else goto invalid_format; - } else if (strcmp (mime_type, "audio/x-raw-int") == 0) { + } else if (g_str_equal (name, "audio/x-raw-int")) { if (sample_size != sizeof (gint16)) goto invalid_format; @@ -437,13 +446,13 @@ gst_rg_analysis_transform_ip (GstBaseTransform * base, GstBuffer * buf) { GstRgAnalysis *filter = GST_RG_ANALYSIS (base); - g_return_val_if_fail (filter->ctx != NULL, GST_FLOW_ERROR); - g_return_val_if_fail (filter->analyze != NULL, GST_FLOW_ERROR); + g_return_val_if_fail (filter->ctx != NULL, GST_FLOW_WRONG_STATE); + g_return_val_if_fail (filter->analyze != NULL, GST_FLOW_NOT_NEGOTIATED); if (filter->skip) return GST_FLOW_OK; - GST_DEBUG_OBJECT (filter, "Processing buffer of size %u", + GST_LOG_OBJECT (filter, "processing buffer of size %u", GST_BUFFER_SIZE (buf)); filter->analyze (filter->ctx, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf), @@ -463,11 +472,11 @@ gst_rg_analysis_event (GstBaseTransform * base, GstEvent * event) case GST_EVENT_EOS: { - GST_DEBUG_OBJECT (filter, "Received EOS event"); + GST_LOG_OBJECT (filter, "received EOS event"); gst_rg_analysis_handle_eos (filter); - GST_DEBUG_OBJECT (filter, "Passing on EOS event"); + GST_LOG_OBJECT (filter, "passing on EOS event"); break; } @@ -498,7 +507,7 @@ gst_rg_analysis_stop (GstBaseTransform * base) rg_analysis_destroy (filter->ctx); filter->ctx = NULL; - GST_DEBUG_OBJECT (filter, "Stopped"); + GST_LOG_OBJECT (filter, "stopped"); return TRUE; } @@ -514,13 +523,13 @@ gst_rg_analysis_handle_tags (GstRgAnalysis * filter, filter->ignore_tags = FALSE; if (filter->skip && album_processing) { - GST_INFO_OBJECT (filter, "Ignoring TAG event: Skipping album"); + GST_DEBUG_OBJECT (filter, "ignoring tag event: skipping album"); return; } else if (filter->skip) { - GST_INFO_OBJECT (filter, "Ignoring TAG event: Skipping track"); + GST_DEBUG_OBJECT (filter, "ignoring tag event: skipping track"); return; } else if (filter->ignore_tags) { - GST_INFO_OBJECT (filter, "Ignoring TAG event: Cannot skip anyways"); + GST_DEBUG_OBJECT (filter, "ignoring tag event: cannot skip anyways"); return; } @@ -534,30 +543,31 @@ gst_rg_analysis_handle_tags (GstRgAnalysis * filter, GST_TAG_ALBUM_PEAK, &dummy); if (!(filter->has_track_gain && filter->has_track_peak)) { - GST_INFO_OBJECT (filter, "Track tags not complete yet"); + GST_DEBUG_OBJECT (filter, "track tags not complete yet"); return; } if (album_processing && !(filter->has_album_gain && filter->has_album_peak)) { - GST_INFO_OBJECT (filter, "Album tags not complete yet"); + GST_DEBUG_OBJECT (filter, "album tags not complete yet"); return; } if (filter->forced) { - GST_INFO_OBJECT (filter, - "Existing tags are sufficient, but processing anyway (forced)"); + GST_DEBUG_OBJECT (filter, + "existing tags are sufficient, but processing anyway (forced)"); return; } filter->skip = TRUE; rg_analysis_reset (filter->ctx); - if (!album_processing) - GST_INFO_OBJECT (filter, - "Existing tags are sufficient, will not process this track"); - else - GST_INFO_OBJECT (filter, - "Existing tags are sufficient, will not process this album"); + if (!album_processing) { + GST_DEBUG_OBJECT (filter, + "existing tags are sufficient, will not process this track"); + } else { + GST_DEBUG_OBJECT (filter, + "existing tags are sufficient, will not process this album"); + } } static void @@ -599,7 +609,9 @@ gst_rg_analysis_handle_eos (GstRgAnalysis * filter) rg_analysis_reset_album (filter->ctx); if (track_success || album_success) { - GST_DEBUG_OBJECT (filter, "Posting tag list with results"); + GST_LOG_OBJECT (filter, "posting tag list with results"); + gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, + GST_TAG_REFERENCE_LEVEL, filter->reference_level, NULL); /* This steals our reference to the list: */ gst_element_found_tags_for_pad (GST_ELEMENT (filter), GST_BASE_TRANSFORM_SRC_PAD (GST_BASE_TRANSFORM (filter)), tag_list); @@ -609,11 +621,12 @@ gst_rg_analysis_handle_eos (GstRgAnalysis * filter) if (album_processing) { filter->num_tracks--; - if (!album_finished) - GST_INFO_OBJECT (filter, "Album not finished yet (num-tracks is now %u)", + if (!album_finished) { + GST_DEBUG_OBJECT (filter, "album not finished yet (num-tracks is now %u)", filter->num_tracks); - else - GST_INFO_OBJECT (filter, "Album finished (num-tracks is now 0)"); + } else { + GST_DEBUG_OBJECT (filter, "album finished (num-tracks is now 0)"); + } } if (album_processing) @@ -631,10 +644,10 @@ gst_rg_analysis_track_result (GstRgAnalysis * filter, GstTagList ** tag_list) if (track_success) { track_gain += filter->reference_level - RG_REFERENCE_LEVEL; - GST_INFO_OBJECT (filter, "Track gain is %+.2f dB, peak %.6f", track_gain, + GST_INFO_OBJECT (filter, "track gain is %+.2f dB, peak %.6f", track_gain, track_peak); } else { - GST_INFO_OBJECT (filter, "Track was too short to analyze"); + GST_INFO_OBJECT (filter, "track was too short to analyze"); } if (track_success) { @@ -658,10 +671,10 @@ gst_rg_analysis_album_result (GstRgAnalysis * filter, GstTagList ** tag_list) if (album_success) { album_gain += filter->reference_level - RG_REFERENCE_LEVEL; - GST_INFO_OBJECT (filter, "Album gain is %+.2f dB, peak %.6f", album_gain, + GST_INFO_OBJECT (filter, "album gain is %+.2f dB, peak %.6f", album_gain, album_peak); } else { - GST_INFO_OBJECT (filter, "Album was too short to analyze"); + GST_INFO_OBJECT (filter, "album was too short to analyze"); } if (album_success) { @@ -673,14 +686,3 @@ gst_rg_analysis_album_result (GstRgAnalysis * filter, GstTagList ** tag_list) return album_success; } - -static gboolean -plugin_init (GstPlugin * plugin) -{ - return gst_element_register (plugin, "rganalysis", GST_RANK_NONE, - GST_TYPE_RG_ANALYSIS); -} - -GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, "replaygain", - "ReplayGain analysis", plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, - GST_PACKAGE_ORIGIN); diff --git a/gst/replaygain/gstrganalysis.h b/gst/replaygain/gstrganalysis.h index 121ce4af..fbf46830 100644 --- a/gst/replaygain/gstrganalysis.h +++ b/gst/replaygain/gstrganalysis.h @@ -78,6 +78,8 @@ struct _GstRgAnalysisClass GstBaseTransformClass parent_class; }; +GType gst_rg_analysis_get_type (void); + G_END_DECLS #endif /* __GST_RG_ANALYSIS_H__ */ diff --git a/gst/replaygain/gstrglimiter.c b/gst/replaygain/gstrglimiter.c new file mode 100644 index 00000000..609db3d7 --- /dev/null +++ b/gst/replaygain/gstrglimiter.c @@ -0,0 +1,197 @@ +/* GStreamer ReplayGain limiter + * + * Copyright (C) 2007 Rene Stadler <mail@renestadler.de> + * + * gstrglimiter.c: Element to apply signal compression to raw audio data + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +/** + * SECTION:element-rglimiter + * @see_also: <link linkend="GstRgVolume">rgvolume</link> + * + * <refsect2> + * <para> + * This element applies signal compression/limiting to raw audio data. It + * performs strict hard limiting with soft-knee characteristics, using a + * threshold of -6 dB. This type of filter is mentioned in the proposed <ulink + * url="http://replaygain.org">ReplayGain standard</ulink>. + * </para> + * <title>Example launch line</title> + * <para>Playback of a file:</para> + * <programlisting> + * gst-launch filesrc location="Filename.ext" ! decodebin ! audioconvert \ + * ! rgvolume pre-amp=6.0 headroom=10.0 ! rglimiter \ + * ! audioconvert ! audioresample ! alsasink + * </programlisting> + * </refsect2> + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <gst/gst.h> +#include <math.h> + +#include "gstrglimiter.h" + +GST_DEBUG_CATEGORY_STATIC (gst_rg_limiter_debug); +#define GST_CAT_DEFAULT gst_rg_limiter_debug + +enum +{ + PROP_0, + PROP_ENABLED, +}; + +static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, " + "width = (int) 32, channels = (int) [1, MAX], " + "rate = (int) [1, MAX], endianness = (int) BYTE_ORDER")); + +static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, " + "width = (int) 32, channels = (int) [1, MAX], " + "rate = (int) [1, MAX], endianness = (int) BYTE_ORDER")); + +GST_BOILERPLATE (GstRgLimiter, gst_rg_limiter, GstBaseTransform, + GST_TYPE_BASE_TRANSFORM); + +static void gst_rg_limiter_class_init (GstRgLimiterClass * klass); +static void gst_rg_limiter_init (GstRgLimiter * filter, + GstRgLimiterClass * gclass); + +static void gst_rg_limiter_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_rg_limiter_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); + +static GstFlowReturn gst_rg_limiter_transform_ip (GstBaseTransform * base, + GstBuffer * buf); + +static const GstElementDetails element_details = { + "ReplayGain limiter", + "Filter/Effect/Audio", + "Apply signal compression to raw audio data", + "Ren\xc3\xa9 Stadler <mail@renestadler.de>" +}; + +static void +gst_rg_limiter_base_init (gpointer g_class) +{ + GstElementClass *element_class = g_class; + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_factory)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_factory)); + gst_element_class_set_details (element_class, &element_details); + + GST_DEBUG_CATEGORY_INIT (gst_rg_limiter_debug, "rglimiter", 0, + "ReplayGain limiter element"); +} + +static void +gst_rg_limiter_class_init (GstRgLimiterClass * klass) +{ + GObjectClass *gobject_class; + GstBaseTransformClass *trans_class; + + gobject_class = (GObjectClass *) klass; + + gobject_class->set_property = gst_rg_limiter_set_property; + gobject_class->get_property = gst_rg_limiter_get_property; + + g_object_class_install_property (gobject_class, PROP_ENABLED, + g_param_spec_boolean ("enabled", "Enabled", "Enable processing", TRUE, + G_PARAM_READWRITE)); + + trans_class = GST_BASE_TRANSFORM_CLASS (klass); + trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_rg_limiter_transform_ip); + trans_class->passthrough_on_same_caps = FALSE; +} + +static void +gst_rg_limiter_init (GstRgLimiter * filter, GstRgLimiterClass * gclass) +{ + filter->enabled = TRUE; + gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), FALSE); +} + +static void +gst_rg_limiter_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstRgLimiter *filter = GST_RG_LIMITER (object); + + switch (prop_id) { + case PROP_ENABLED: + filter->enabled = g_value_get_boolean (value); + gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (filter), + !filter->enabled); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_rg_limiter_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstRgLimiter *filter = GST_RG_LIMITER (object); + + switch (prop_id) { + case PROP_ENABLED: + g_value_set_boolean (value, filter->enabled); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +#define LIMIT 1.0 +#define THRES 0.5 /* ca. -6 dB */ +#define COMPL 0.5 /* LIMIT - THRESH */ + +static GstFlowReturn +gst_rg_limiter_transform_ip (GstBaseTransform * base, GstBuffer * buf) +{ + GstRgLimiter *filter = GST_RG_LIMITER (base); + gfloat *input; + guint count; + guint i; + + if (!filter->enabled) + return GST_FLOW_OK; + + input = (gfloat *) GST_BUFFER_DATA (buf); + count = GST_BUFFER_SIZE (buf) / sizeof (gfloat); + + for (i = count; i--;) { + if (*input > THRES) + *input = tanhf ((*input - THRES) / COMPL) * COMPL + THRES; + else if (*input < -THRES) + *input = tanhf ((*input + THRES) / COMPL) * COMPL - THRES; + input++; + } + + return GST_FLOW_OK; +} diff --git a/gst/replaygain/gstrglimiter.h b/gst/replaygain/gstrglimiter.h new file mode 100644 index 00000000..63bd8049 --- /dev/null +++ b/gst/replaygain/gstrglimiter.h @@ -0,0 +1,64 @@ +/* GStreamer ReplayGain limiter + * + * Copyright (C) 2007 Rene Stadler <mail@renestadler.de> + * + * gstrglimiter.h: Element to apply signal compression to raw audio data + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef __GST_RG_LIMITER_H__ +#define __GST_RG_LIMITER_H__ + +#include <gst/gst.h> +#include <gst/base/gstbasetransform.h> + +#define GST_TYPE_RG_LIMITER \ + (gst_rg_limiter_get_type()) +#define GST_RG_LIMITER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RG_LIMITER,GstRgLimiter)) +#define GST_RG_LIMITER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RG_LIMITER,GstRgLimiterClass)) +#define GST_IS_RG_LIMITER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RG_LIMITER)) +#define GST_IS_RG_LIMITER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RG_LIMITER)) + +typedef struct _GstRgLimiter GstRgLimiter; +typedef struct _GstRgLimiterClass GstRgLimiterClass; + +/** + * GstRgLimiter: + * + * Opaque data structure. + */ +struct _GstRgLimiter +{ + GstBaseTransform element; + + /*< private >*/ + + gboolean enabled; +}; + +struct _GstRgLimiterClass +{ + GstBaseTransformClass parent_class; +}; + +GType gst_rg_limiter_get_type (void); + +#endif /* __GST_RG_LIMITER_H__ */ diff --git a/gst/replaygain/gstrgvolume.c b/gst/replaygain/gstrgvolume.c new file mode 100644 index 00000000..35b4f5ef --- /dev/null +++ b/gst/replaygain/gstrgvolume.c @@ -0,0 +1,702 @@ +/* GStreamer ReplayGain volume adjustment + * + * Copyright (C) 2007 Rene Stadler <mail@renestadler.de> + * + * gstrgvolume.c: Element to apply ReplayGain volume adjustment + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +/** + * SECTION:element-rgvolume + * @see_also: <link linkend="GstRgLimiter">rglimiter</link>, + * <link linkend="GstRgAnalysis">rganalysis</link> + * + * <refsect2> + * <para> + * This element applies volume changes to streams as lined out in the proposed + * <ulink url="http://replaygain.org">ReplayGain standard</ulink>. It + * interprets the ReplayGain meta data tags and carries out the adjustment (by + * using a volume element internally). The relevant tags are: + * <itemizedlist> + * <listitem>#GST_TAG_TRACK_GAIN</listitem> + * <listitem>#GST_TAG_TRACK_PEAK</listitem> + * <listitem>#GST_TAG_ALBUM_GAIN</listitem> + * <listitem>#GST_TAG_ALBUM_PEAK</listitem> + * <listitem>#GST_TAG_REFERENCE_LEVEL</listitem> + * </itemizedlist> + * The information carried by these tags must have been calculated beforehand by + * performing the ReplayGain analysis. This is implemented by the <link + * linkend="GstRgAnalysis">rganalysis</link> element. + * </para> + * <para> + * The signal compression/limiting recommendations outlined in the proposed + * standard are not implemented by this element. This has to be handled by + * separate elements because applications might want to have additional filters + * between the volume adjustment and the limiting stage. A basic limiter is + * included with this plugin: The <link linkend="GstRgLimiter">rglimiter</link> + * element applies -6 dB hard limiting as mentioned in the ReplayGain standard. + * </para> + * <title>Example launch line</title> + * <para>Playback of a file:</para> + * <programlisting> + * gst-launch filesrc location="Filename.ext" ! decodebin ! audioconvert \ + * ! rgvolume ! audioconvert ! audioresample ! alsasink + * </programlisting> + * </refsect2> + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <gst/gst.h> +#include <gst/pbutils/pbutils.h> +#include <math.h> + +#include "gstrgvolume.h" +#include "replaygain.h" + +GST_DEBUG_CATEGORY_STATIC (gst_rg_volume_debug); +#define GST_CAT_DEFAULT gst_rg_volume_debug + +enum +{ + PROP_0, + PROP_ALBUM_MODE, + PROP_HEADROOM, + PROP_PRE_AMP, + PROP_FALLBACK_GAIN, + PROP_TARGET_GAIN, + PROP_RESULT_GAIN +}; + +#define DEFAULT_ALBUM_MODE TRUE +#define DEFAULT_HEADROOM 0.0 +#define DEFAULT_PRE_AMP 0.0 +#define DEFAULT_FALLBACK_GAIN 0.0 + +#define DB_TO_LINEAR(x) pow (10., (x) / 20.) +#define LINEAR_TO_DB(x) (20. * log10 (x)) + +#define GAIN_FORMAT "+.02f dB" +#define PEAK_FORMAT ".06f" + +#define VALID_GAIN(x) ((x) > -60.00 && (x) < 60.00) +#define VALID_PEAK(x) ((x) > 0.) + +/* Same template caps as GstVolume, for I don't like having just ANY caps. */ + +static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, " + "rate = (int) [ 1, MAX ], " + "channels = (int) [ 1, MAX ], " + "endianness = (int) BYTE_ORDER, " + "width = (int) 32; " + "audio/x-raw-int, " + "channels = (int) [ 1, MAX ], " + "rate = (int) [ 1, MAX ], " + "endianness = (int) BYTE_ORDER, " + "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE")); + +static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, " + "rate = (int) [ 1, MAX ], " + "channels = (int) [ 1, MAX ], " + "endianness = (int) BYTE_ORDER, " + "width = (int) 32; " + "audio/x-raw-int, " + "channels = (int) [ 1, MAX ], " + "rate = (int) [ 1, MAX ], " + "endianness = (int) BYTE_ORDER, " + "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE")); + +GST_BOILERPLATE (GstRgVolume, gst_rg_volume, GstBin, GST_TYPE_BIN); + +static void gst_rg_volume_class_init (GstRgVolumeClass * klass); +static void gst_rg_volume_init (GstRgVolume * self, GstRgVolumeClass * gclass); + +static void gst_rg_volume_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_rg_volume_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); +static void gst_rg_volume_dispose (GObject * object); + +static GstStateChangeReturn gst_rg_volume_change_state (GstElement * element, + GstStateChange transition); +static gboolean gst_rg_volume_sink_event (GstPad * pad, GstEvent * event); + +static GstEvent *gst_rg_volume_tag_event (GstRgVolume * self, GstEvent * event); +static void gst_rg_volume_reset (GstRgVolume * self); +static void gst_rg_volume_update_gain (GstRgVolume * self); +static inline void gst_rg_volume_determine_gain (GstRgVolume * self, + gdouble * target_gain, gdouble * result_gain); + +static void +gst_rg_volume_base_init (gpointer g_class) +{ + GstElementClass *element_class = g_class; + + static const GstElementDetails element_details = { + "ReplayGain volume", + "Filter/Effect/Audio", + "Apply ReplayGain volume adjustment", + "Ren\xc3\xa9 Stadler <mail@renestadler.de>" + }; + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_template)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_template)); + gst_element_class_set_details (element_class, &element_details); + + GST_DEBUG_CATEGORY_INIT (gst_rg_volume_debug, "rgvolume", 0, + "ReplayGain volume element"); +} + +static void +gst_rg_volume_class_init (GstRgVolumeClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *element_class; + GstBinClass *bin_class; + + gobject_class = (GObjectClass *) klass; + + gobject_class->set_property = gst_rg_volume_set_property; + gobject_class->get_property = gst_rg_volume_get_property; + gobject_class->dispose = gst_rg_volume_dispose; + + /** + * GstRgVolume:album-mode: + * + * Whether to prefer album gain over track gain. + * + * If set to %TRUE, use album gain instead of track gain if both are + * available. This keeps the relative loudness levels of tracks from the same + * album intact. + * + * If set to %FALSE, track mode is used instead. This effectively leads to + * more extensive normalization. + * + * If album mode is enabled but the album gain tag is absent in the stream, + * the track gain is used instead. If both gain tags are missing, the value + * of the <link linkend="GstRgVolume--fallback-gain">fallback-gain</link> + * property is used instead. + */ + g_object_class_install_property (gobject_class, PROP_ALBUM_MODE, + g_param_spec_boolean ("album-mode", "Album mode", + "Prefer album over track gain", DEFAULT_ALBUM_MODE, + G_PARAM_READWRITE)); + /** + * GstRgVolume:headroom: + * + * Extra headroom [dB]. This controls the amount by which the output can + * exceed digital full scale. + * + * Only set this to a value greater than 0.0 if signal compression/limiting of + * a suitable form is applied to the output (or output is brought into the + * correct range by some other transformation). + * + * This element internally uses a volume element, which also supports + * operating on integer audio formats. These formats do not allow exceeding + * digital full scale. If extra headroom is used, make sure that the raw + * audio data format is floating point (audio/x-raw-float). Otherwise, + * clipping distortion might be introduced as part of the volume adjustment + * itself. + */ + g_object_class_install_property (gobject_class, PROP_HEADROOM, + g_param_spec_double ("headroom", "Headroom", "Extra headroom [dB]", + 0., 60., DEFAULT_HEADROOM, G_PARAM_READWRITE)); + /** + * GstRgVolume:pre-amp: + * + * Additional gain to apply globally [dB]. This controls the trade-off + * between uniformity of normalization and utilization of available dynamic + * range. + * + * Note that the default value is 0 dB because the ReplayGain reference value + * was adjusted by +6 dB (from 83 to 89 dB). At the time of this writing, the + * <ulink url="http://replaygain.org">webpage</ulink> is still outdated and + * does not reflect this change however. Where the original proposal states + * that a proper default pre-amp value is +6 dB, this translates to the used 0 + * dB. + */ + g_object_class_install_property (gobject_class, PROP_PRE_AMP, + g_param_spec_double ("pre-amp", "Pre-amp", "Extra gain [dB]", + -60., 60., DEFAULT_PRE_AMP, G_PARAM_READWRITE)); + /** + * GstRgVolume:fallback-gain: + * + * Fallback gain [dB] for streams missing ReplayGain tags. + */ + g_object_class_install_property (gobject_class, PROP_FALLBACK_GAIN, + g_param_spec_double ("fallback-gain", "Fallback gain", + "Gain for streams missing tags [dB]", + -60., 60., DEFAULT_FALLBACK_GAIN, G_PARAM_READWRITE)); + /** + * GstRgVolume:result-gain: + * + * Applied gain [dB]. This gain is applied to processed buffer data. + * + * This is set to the <link linkend="GstRgVolume--target-gain">target + * gain</link> if amplification by that amount can be applied safely. + * "Safely" means that the volume adjustment does not inflict clipping + * distortion. Should this not be the case, the result gain is set to an + * appropriately reduced value (by applying peak normalization). The proposed + * standard calls this "clipping prevention". + * + * The difference between target and result gain reflects the necessary amount + * of reduction. Applications can make use of this information to temporarily + * reduce the <link linkend="GstRgVolume--pre-amp">pre-amp</link> for + * subsequent streams, as recommended by the ReplayGain standard. + * + * Note that target and result gain differing for a great majority of streams + * indicates a problem: What happens in this case is that most streams receive + * peak normalization instead of amplification by the ideal replay gain. To + * prevent this, the <link linkend="GstRgVolume--pre-amp">pre-amp</link> has + * to be lowered and/or a limiter has to be used which facilitates the use of + * <link linkend="GstRgVolume--headroom">headroom</link>. + */ + g_object_class_install_property (gobject_class, PROP_RESULT_GAIN, + g_param_spec_double ("result-gain", "Result-gain", "Applied gain [dB]", + -120., 120., 0., G_PARAM_READABLE)); + /** + * GstRgVolume:target-gain: + * + * Applicable gain [dB]. This gain is supposed to be applied. + * + * Depending on the value of the <link + * linkend="GstRgVolume--album-mode">album-mode</link> property and the + * presence of ReplayGain tags in the stream, this is set according to one of + * these simple formulas: + * + * <itemizedlist> + * <listitem><link linkend="GstRgVolume--pre-amp">pre-amp</link> + album gain + * of the stream</listitem> + * <listitem><link linkend="GstRgVolume--pre-amp">pre-amp</link> + track gain + * of the stream</listitem> + * <listitem><link linkend="GstRgVolume--pre-amp">pre-amp</link> + <link + * linkend="GstRgVolume--fallback-gain">fallback gain</link></listitem> + * </itemizedlist> + */ + g_object_class_install_property (gobject_class, PROP_TARGET_GAIN, + g_param_spec_double ("target-gain", "Target-gain", + "Applicable gain [dB]", -120., 120., 0., G_PARAM_READABLE)); + + element_class = (GstElementClass *) klass; + element_class->change_state = GST_DEBUG_FUNCPTR (gst_rg_volume_change_state); + + bin_class = (GstBinClass *) klass; + /* Setting these to NULL makes gst_bin_add and _remove refuse to let anyone + * mess with our internals. */ + bin_class->add_element = NULL; + bin_class->remove_element = NULL; +} + +static void +gst_rg_volume_init (GstRgVolume * self, GstRgVolumeClass * gclass) +{ + GObjectClass *volume_class; + GstPad *volume_pad, *ghost_pad; + + self->album_mode = DEFAULT_ALBUM_MODE; + self->headroom = DEFAULT_HEADROOM; + self->pre_amp = DEFAULT_PRE_AMP; + self->fallback_gain = DEFAULT_FALLBACK_GAIN; + self->target_gain = 0.0; + self->result_gain = 0.0; + + self->volume_element = gst_element_factory_make ("volume", "rgvolume-volume"); + if (G_UNLIKELY (self->volume_element == NULL)) { + GstMessage *msg; + + GST_WARNING_OBJECT (self, "could not create volume element"); + msg = gst_missing_element_message_new (GST_ELEMENT_CAST (self), "volume"); + gst_element_post_message (GST_ELEMENT_CAST (self), msg); + + /* Nothing else to do, we will refuse the state change from NULL to READY to + * indicate that something went very wrong. It is doubtful that someone + * attempts changing our state though, since we end up having no pads! */ + return; + } + + volume_class = G_OBJECT_GET_CLASS (G_OBJECT (self->volume_element)); + self->max_volume = G_PARAM_SPEC_DOUBLE + (g_object_class_find_property (volume_class, "volume"))->maximum; + + GST_BIN_CLASS (parent_class)->add_element (GST_BIN_CAST (self), + self->volume_element); + + volume_pad = gst_element_get_pad (self->volume_element, "sink"); + ghost_pad = gst_ghost_pad_new_from_template ("sink", volume_pad, + gst_pad_get_pad_template (volume_pad)); + gst_object_unref (volume_pad); + gst_pad_set_event_function (ghost_pad, gst_rg_volume_sink_event); + gst_element_add_pad (GST_ELEMENT_CAST (self), ghost_pad); + + volume_pad = gst_element_get_pad (self->volume_element, "src"); + ghost_pad = gst_ghost_pad_new_from_template ("src", volume_pad, + gst_pad_get_pad_template (volume_pad)); + gst_object_unref (volume_pad); + gst_element_add_pad (GST_ELEMENT_CAST (self), ghost_pad); +} + +static void +gst_rg_volume_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstRgVolume *self = GST_RG_VOLUME (object); + + switch (prop_id) { + case PROP_ALBUM_MODE: + self->album_mode = g_value_get_boolean (value); + break; + case PROP_HEADROOM: + self->headroom = g_value_get_double (value); + break; + case PROP_PRE_AMP: + self->pre_amp = g_value_get_double (value); + break; + case PROP_FALLBACK_GAIN: + self->fallback_gain = g_value_get_double (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } + + gst_rg_volume_update_gain (self); +} + +static void +gst_rg_volume_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstRgVolume *self = GST_RG_VOLUME (object); + + switch (prop_id) { + case PROP_ALBUM_MODE: + g_value_set_boolean (value, self->album_mode); + break; + case PROP_HEADROOM: + g_value_set_double (value, self->headroom); + break; + case PROP_PRE_AMP: + g_value_set_double (value, self->pre_amp); + break; + case PROP_FALLBACK_GAIN: + g_value_set_double (value, self->fallback_gain); + break; + case PROP_TARGET_GAIN: + g_value_set_double (value, self->target_gain); + break; + case PROP_RESULT_GAIN: + g_value_set_double (value, self->result_gain); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_rg_volume_dispose (GObject * object) +{ + GstRgVolume *self = GST_RG_VOLUME (object); + + if (self->volume_element != NULL) { + /* Manually remove our child using the bin implementation of remove_element. + * This is needed because we prevent gst_bin_remove from working, which the + * parent dispose handler would use if we had any children left. */ + GST_BIN_CLASS (parent_class)->remove_element (GST_BIN_CAST (self), + self->volume_element); + self->volume_element = NULL; + } + + G_OBJECT_CLASS (parent_class)->dispose (object); +} + +static GstStateChangeReturn +gst_rg_volume_change_state (GstElement * element, GstStateChange transition) +{ + GstRgVolume *self = GST_RG_VOLUME (element); + GstStateChangeReturn res; + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + + if (G_UNLIKELY (self->volume_element == NULL)) { + /* Creating our child volume element in _init failed. */ + return GST_STATE_CHANGE_FAILURE; + } + break; + + case GST_STATE_CHANGE_READY_TO_PAUSED: + + gst_rg_volume_reset (self); + break; + + default: + break; + } + + res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + + return res; +} + +/* Event function for the ghost sink pad. */ +static gboolean +gst_rg_volume_sink_event (GstPad * pad, GstEvent * event) +{ + GstRgVolume *self; + GstPad *volume_sink_pad; + GstEvent *send_event = event; + gboolean res; + + self = GST_RG_VOLUME (gst_pad_get_parent_element (pad)); + volume_sink_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad)); + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_TAG: + + GST_LOG_OBJECT (self, "received tag event"); + + send_event = gst_rg_volume_tag_event (self, event); + + if (send_event == NULL) + GST_LOG_OBJECT (self, "all tags handled, dropping event"); + + break; + + case GST_EVENT_EOS: + + gst_rg_volume_reset (self); + break; + + default: + break; + } + + if (G_LIKELY (send_event != NULL)) + res = gst_pad_send_event (volume_sink_pad, send_event); + else + res = TRUE; + + gst_object_unref (volume_sink_pad); + gst_object_unref (self); + return res; +} + +static GstEvent * +gst_rg_volume_tag_event (GstRgVolume * self, GstEvent * event) +{ + GstTagList *tag_list; + gboolean has_track_gain, has_track_peak, has_album_gain, has_album_peak; + gboolean has_ref_level; + + g_return_val_if_fail (event != NULL, NULL); + g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TAG, event); + + gst_event_parse_tag (event, &tag_list); + + if (gst_tag_list_is_empty (tag_list)) + return event; + + has_track_gain = gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, + &self->track_gain); + has_track_peak = gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, + &self->track_peak); + has_album_gain = gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, + &self->album_gain); + has_album_peak = gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, + &self->album_peak); + has_ref_level = gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL, + &self->reference_level); + + if (!has_track_gain && !has_track_peak && !has_album_gain && !has_album_peak) + return event; + + if (has_ref_level && (has_track_gain || has_album_gain) + && (ABS (self->reference_level - RG_REFERENCE_LEVEL) > 1.e-6)) { + /* Log a message stating the amount of adjustment that is applied below. */ + GST_DEBUG_OBJECT (self, + "compensating for reference level difference by %" GAIN_FORMAT, + RG_REFERENCE_LEVEL - self->reference_level); + } + if (has_track_gain) { + self->track_gain += RG_REFERENCE_LEVEL - self->reference_level; + } + if (has_album_gain) { + self->album_gain += RG_REFERENCE_LEVEL - self->reference_level; + } + + /* Ignore values that are obviously invalid. */ + if (G_UNLIKELY (has_track_gain && !VALID_GAIN (self->track_gain))) { + GST_DEBUG_OBJECT (self, + "ignoring bogus track gain value %" GAIN_FORMAT, self->track_gain); + has_track_gain = FALSE; + } + if (G_UNLIKELY (has_track_peak && !VALID_PEAK (self->track_peak))) { + GST_DEBUG_OBJECT (self, + "ignoring bogus track peak value %" PEAK_FORMAT, self->track_peak); + has_track_peak = FALSE; + } + if (G_UNLIKELY (has_album_gain && !VALID_GAIN (self->album_gain))) { + GST_DEBUG_OBJECT (self, + "ignoring bogus album gain value %" GAIN_FORMAT, self->album_gain); + has_album_gain = FALSE; + } + if (G_UNLIKELY (has_album_peak && !VALID_PEAK (self->album_peak))) { + GST_DEBUG_OBJECT (self, + "ignoring bogus album peak value %" PEAK_FORMAT, self->album_peak); + has_album_peak = FALSE; + } + + self->has_track_gain |= has_track_gain; + self->has_track_peak |= has_track_peak; + self->has_album_gain |= has_album_gain; + self->has_album_peak |= has_album_peak; + + event = (GstEvent *) gst_mini_object_make_writable (GST_MINI_OBJECT (event)); + gst_event_parse_tag (event, &tag_list); + + gst_tag_list_remove_tag (tag_list, GST_TAG_TRACK_GAIN); + gst_tag_list_remove_tag (tag_list, GST_TAG_TRACK_PEAK); + gst_tag_list_remove_tag (tag_list, GST_TAG_ALBUM_GAIN); + gst_tag_list_remove_tag (tag_list, GST_TAG_ALBUM_PEAK); + gst_tag_list_remove_tag (tag_list, GST_TAG_REFERENCE_LEVEL); + + gst_rg_volume_update_gain (self); + + if (gst_tag_list_is_empty (tag_list)) { + gst_event_unref (event); + event = NULL; + } + + return event; +} + +static void +gst_rg_volume_reset (GstRgVolume * self) +{ + self->has_track_gain = FALSE; + self->has_track_peak = FALSE; + self->has_album_gain = FALSE; + self->has_album_peak = FALSE; + + self->reference_level = RG_REFERENCE_LEVEL; + + gst_rg_volume_update_gain (self); +} + +static void +gst_rg_volume_update_gain (GstRgVolume * self) +{ + gdouble target_gain, result_gain, result_volume; + gboolean target_gain_changed, result_gain_changed; + + gst_rg_volume_determine_gain (self, &target_gain, &result_gain); + + result_volume = DB_TO_LINEAR (result_gain); + + /* Ensure that the result volume is within the range that the volume element + * can handle. Currently, the limit is 10. (+20 dB), which should not be + * restrictive. */ + if (G_UNLIKELY (result_volume > self->max_volume)) { + GST_INFO_OBJECT (self, + "cannot handle result gain of %" GAIN_FORMAT " (%0.6f), adjusting", + result_gain, result_volume); + + result_volume = self->max_volume; + result_gain = LINEAR_TO_DB (result_volume); + } + + /* Direct comparison is OK in this case. */ + if (target_gain == result_gain) { + GST_INFO_OBJECT (self, + "result gain is %" GAIN_FORMAT " (%0.6f), matching target", + result_gain, result_volume); + } else { + GST_INFO_OBJECT (self, + "result gain is %" GAIN_FORMAT " (%0.6f), target is %" GAIN_FORMAT, + result_gain, result_volume, target_gain); + } + + target_gain_changed = (self->target_gain != target_gain); + result_gain_changed = (self->result_gain != result_gain); + + self->target_gain = target_gain; + self->result_gain = result_gain; + + g_object_set (self->volume_element, "volume", result_volume, NULL); + + if (target_gain_changed) + g_object_notify ((GObject *) self, "target-gain"); + if (result_gain_changed) + g_object_notify ((GObject *) self, "result-gain"); +} + +static inline void +gst_rg_volume_determine_gain (GstRgVolume * self, gdouble * target_gain, + gdouble * result_gain) +{ + gdouble gain, peak; + + if (!self->has_track_gain && !self->has_album_gain) { + + GST_DEBUG_OBJECT (self, "using fallback gain"); + gain = self->fallback_gain; + peak = 1.0; + + } else if ((self->album_mode && self->has_album_gain) + || (!self->album_mode && !self->has_track_gain)) { + + gain = self->album_gain; + if (G_LIKELY (self->has_album_peak)) { + peak = self->album_peak; + } else { + GST_DEBUG_OBJECT (self, "album peak missing, assuming 1.0"); + peak = 1.0; + } + /* Falling back from track to album gain shouldn't really happen. */ + if (G_UNLIKELY (!self->album_mode)) + GST_INFO_OBJECT (self, "falling back to album gain"); + + } else { + /* !album_mode && !has_album_gain || album_mode && has_track_gain */ + + gain = self->track_gain; + if (G_LIKELY (self->has_track_peak)) { + peak = self->track_peak; + } else { + GST_DEBUG_OBJECT (self, "track peak missing, assuming 1.0"); + peak = 1.0; + } + if (self->album_mode) + GST_INFO_OBJECT (self, "falling back to track gain"); + } + + gain += self->pre_amp; + + *target_gain = gain; + *result_gain = gain; + + if (LINEAR_TO_DB (peak) + gain > self->headroom) { + *result_gain = LINEAR_TO_DB (1. / peak) + self->headroom; + } +} diff --git a/gst/replaygain/gstrgvolume.h b/gst/replaygain/gstrgvolume.h new file mode 100644 index 00000000..8fc29614 --- /dev/null +++ b/gst/replaygain/gstrgvolume.h @@ -0,0 +1,88 @@ +/* GStreamer ReplayGain volume adjustment + * + * Copyright (C) 2007 Rene Stadler <mail@renestadler.de> + * + * gstrgvolume.h: Element to apply ReplayGain volume adjustment + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef __GST_RG_VOLUME_H__ +#define __GST_RG_VOLUME_H__ + +#include <gst/gst.h> + +G_BEGIN_DECLS + +#define GST_TYPE_RG_VOLUME \ + (gst_rg_volume_get_type()) +#define GST_RG_VOLUME(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RG_VOLUME,GstRgVolume)) +#define GST_RG_VOLUME_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RG_VOLUME,GstRgVolumeClass)) +#define GST_IS_PLUGIN_TEMPLATE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RG_VOLUME)) +#define GST_IS_PLUGIN_TEMPLATE_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RG_VOLUME)) + +typedef struct _GstRgVolume GstRgVolume; +typedef struct _GstRgVolumeClass GstRgVolumeClass; + +/** + * GstRgVolume: + * + * Opaque data structure. + */ +struct _GstRgVolume +{ + GstBin bin; + + /*< private >*/ + + GstElement *volume_element; + gdouble max_volume; + + gboolean album_mode; + gdouble headroom; + gdouble pre_amp; + gdouble fallback_gain; + + gdouble target_gain; + gdouble result_gain; + + gdouble track_gain; + gdouble track_peak; + gdouble album_gain; + gdouble album_peak; + + gboolean has_track_gain; + gboolean has_track_peak; + gboolean has_album_gain; + gboolean has_album_peak; + + gdouble reference_level; +}; + +struct _GstRgVolumeClass +{ + GstBinClass parent_class; +}; + +GType gst_rg_volume_get_type (void); + +G_END_DECLS + +#endif /* __GST_RG_VOLUME_H__ */ diff --git a/gst/replaygain/replaygain.c b/gst/replaygain/replaygain.c new file mode 100644 index 00000000..d0127e8b --- /dev/null +++ b/gst/replaygain/replaygain.c @@ -0,0 +1,53 @@ +/* GStreamer ReplayGain plugin + * + * Copyright (C) 2006 Rene Stadler <mail@renestadler.de> + * + * replaygain.c: Plugin providing ReplayGain related elements + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <gst/gst.h> + +#include "gstrganalysis.h" +#include "gstrglimiter.h" +#include "gstrgvolume.h" + +static gboolean +plugin_init (GstPlugin * plugin) +{ + if (!gst_element_register (plugin, "rganalysis", GST_RANK_NONE, + GST_TYPE_RG_ANALYSIS)) + return FALSE; + + if (!gst_element_register (plugin, "rglimiter", GST_RANK_NONE, + GST_TYPE_RG_LIMITER)) + return FALSE; + + if (!gst_element_register (plugin, "rgvolume", GST_RANK_NONE, + GST_TYPE_RG_VOLUME)) + return FALSE; + + return TRUE; +} + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, "replaygain", + "ReplayGain volume normalization", plugin_init, VERSION, GST_LICENSE, + GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN); diff --git a/gst/replaygain/replaygain.h b/gst/replaygain/replaygain.h new file mode 100644 index 00000000..15be8885 --- /dev/null +++ b/gst/replaygain/replaygain.h @@ -0,0 +1,36 @@ +/* GStreamer ReplayGain plugin + * + * Copyright (C) 2006 Rene Stadler <mail@renestadler.de> + * + * replaygain.h: Plugin providing ReplayGain related elements + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef __REPLAYGAIN_H__ +#define __REPLAYGAIN_H__ + +G_BEGIN_DECLS + +/* Reference level (in dBSPL). The 2001 proposal specifies 83. This was + * changed later in all implementations to 89, which is the new, offical value: + * David Robinson acknowledged the change but didn't update the website yet. */ + +#define RG_REFERENCE_LEVEL 89. + +G_END_DECLS + +#endif /* __REPLAYGAIN_H__ */ diff --git a/gst/replaygain/rganalysis.h b/gst/replaygain/rganalysis.h index 39bf9b41..16247361 100644 --- a/gst/replaygain/rganalysis.h +++ b/gst/replaygain/rganalysis.h @@ -29,8 +29,6 @@ G_BEGIN_DECLS -#define RG_REFERENCE_LEVEL 89. - typedef struct _RgAnalysisCtx RgAnalysisCtx; RgAnalysisCtx *rg_analysis_new (void); diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index bb2540f9..d23611c9 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -57,9 +57,12 @@ VALGRIND_TESTS_DISABLE = \ $(VALGRIND_TO_FIX) check_PROGRAMS = \ + elements/deinterleave \ $(check_mpeg2enc) \ $(check_neon) \ elements/rganalysis \ + elements/rglimiter \ + elements/rgvolume \ elements/videocrop \ $(check_wavpack) \ elements/y4menc @@ -72,3 +75,6 @@ LDADD = $(GST_OBJ_LIBS) $(GST_CHECK_LIBS) $(CHECK_LIBS) elements_videocrop_LDADD = $(LDADD) $(GST_BASE_LIBS) elements_videocrop_CFLAGS = $(CFLAGS) $(AM_CFLAGS) $(GST_BASE_CFLAGS) + +elements_deinterleave_LDADD = $(LDADD) $(GST_PLUGINS_BASE_LIBS) -lgstaudio-$(GST_MAJORMINOR) +elements_deinterleave_CFLAGS = $(CFLAGS) $(AM_CFLAGS) $(GST_PLUGINS_BASE_CFLAGS) diff --git a/tests/check/elements/.gitignore b/tests/check/elements/.gitignore index a9c39de6..69674ef1 100644 --- a/tests/check/elements/.gitignore +++ b/tests/check/elements/.gitignore @@ -1,8 +1,10 @@ .dirstamp -tagid3v2mux +deinterleave gdpdepay gdppay mpeg2enc +rglimiter +rgvolume wavpackdec wavpackenc wavpackparse diff --git a/tests/check/elements/rganalysis.c b/tests/check/elements/rganalysis.c index 63e3c720..e8a9db2f 100644 --- a/tests/check/elements/rganalysis.c +++ b/tests/check/elements/rganalysis.c @@ -20,77 +20,72 @@ * 02110-1301 USA */ -/* Some things to note about the RMS window length of the analysis - * algorithm and thus the implementation used in the element: - * Processing divides input data into 50ms windows at some point. - * Some details about this that normally do not matter: +/* Some things to note about the RMS window length of the analysis algorithm and + * thus the implementation used in the element: Processing divides input data + * into 50ms windows at some point. Some details about this that normally do + * not matter: * - * 1. At the end of a stream, the remainder of data that did not fill - * up the last 50ms window is simply discarded. + * 1. At the end of a stream, the remainder of data that did not fill up the + * last 50ms window is simply discarded. * - * 2. If the sample rate changes during a stream, the currently - * running window is discarded and the equal loudness filter gets - * reset as if a new stream started. + * 2. If the sample rate changes during a stream, the currently running window + * is discarded and the equal loudness filter gets reset as if a new stream + * started. * - * 3. For the album gain, it is not entirely correct to think of - * obtaining it like "as if all the tracks are analyzed as one - * track". There isn't a separate window being tracked for album - * processing, so at stream (track) end, the remaining unfilled - * window does not contribute to the album gain either. + * 3. For the album gain, it is not entirely correct to think of obtaining it + * like "as if all the tracks are analyzed as one track". There isn't a + * separate window being tracked for album processing, so at stream (track) + * end, the remaining unfilled window does not contribute to the album gain + * either. * - * 4. If a waveform with a result gain G is concatenated to itself - * and the result processed as a track, the gain can be different - * from G if and only if the duration of the original waveform is - * not an integer multiple of 50ms. If the original waveform gets - * processed as a single track and then the same data again as a - * subsequent track, the album result gain will always match G - * (this is implied by 3.). + * 4. If a waveform with a result gain G is concatenated to itself and the + * result processed as a track, the gain can be different from G if and only + * if the duration of the original waveform is not an integer multiple of + * 50ms. If the original waveform gets processed as a single track and then + * the same data again as a subsequent track, the album result gain will + * always match G (this is implied by 3.). * - * 5. A stream shorter than 50ms cannot be analyzed. At 8000 and - * 48000 Hz, this corresponds to 400 resp. 2400 frames. If a - * stream is shorter than 50ms, the element will not generate tags - * at EOS (only if an album finished, but only album tags are - * generated then). This is not an erroneous condition, the - * element should behave normally. + * 5. A stream shorter than 50ms cannot be analyzed. At 8000 and 48000 Hz, + * this corresponds to 400 resp. 2400 frames. If a stream is shorter than + * 50ms, the element will not generate tags at EOS (only if an album + * finished, but only album tags are generated then). This is not an + * erroneous condition, the element should behave normally. * - * The limitations outlined in 1.-4. do not apply to the peak values. - * Every single sample is accounted for when looking for the peak. - * Thus the album peak is guaranteed to be the maximum value of all - * track peaks. + * The limitations outlined in 1.-4. do not apply to the peak values. Every + * single sample is accounted for when looking for the peak. Thus the album + * peak is guaranteed to be the maximum value of all track peaks. * - * In normal day-to-day use, these little facts are unlikely to be - * relevant, but they have to be kept in mind for writing the tests - * here. + * In normal day-to-day use, these little facts are unlikely to be relevant, but + * they have to be kept in mind for writing the tests here. */ #include <gst/check/gstcheck.h> GList *buffers = NULL; -/* For ease of programming we use globals to keep refs for our floating - * src and sink pads we create; otherwise we always have to do get_pad, - * get_peer, and then remove references in every test function */ +/* For ease of programming we use globals to keep refs for our floating src and + * sink pads we create; otherwise we always have to do get_pad, get_peer, and + * then remove references in every test function */ static GstPad *mysrcpad, *mysinkpad; -/* Mapping from supported sample rates to the correct result gain for - * the following test waveform: 20 * 512 samples with a quarter-full - * amplitude of toggling sign, changing every 48 samples and starting - * with the positive value. +/* Mapping from supported sample rates to the correct result gain for the + * following test waveform: 20 * 512 samples with a quarter-full amplitude of + * toggling sign, changing every 48 samples and starting with the positive + * value. * - * Even if we would generate a wave describing a signal with the same - * frequency at each sampling rate, the results would vary (slightly). - * Hence the simple generation method, since we cannot use a constant - * value as expected result anyways. For all sample rates, changing - * the sign every 48 frames gives a sane frequency. Buffers - * containing data that forms such a waveform is created using the - * test_buffer_square_{float,int16}_{mono,stereo} functions below. + * Even if we would generate a wave describing a signal with the same frequency + * at each sampling rate, the results would vary (slightly). Hence the simple + * generation method, since we cannot use a constant value as expected result + * anyways. For all sample rates, changing the sign every 48 frames gives a + * sane frequency. Buffers containing data that forms such a waveform is + * created using the test_buffer_square_{float,int16}_{mono,stereo} functions + * below. * - * The results have been checked against what the metaflac and - * wavegain programs generate for such a stream. If you want to - * verify these, be sure that the metaflac program does not produce - * incorrect results in your environment: I found a strange bug in the - * (defacto) reference code for the analysis that sometimes leads to - * incorrect RMS window lengths. */ + * The results have been checked against what the metaflac and wavegain programs + * generate for such a stream. If you want to verify these, be sure that the + * metaflac program does not produce incorrect results in your environment: I + * found a strange bug in the (defacto) reference code for the analysis that + * sometimes leads to incorrect RMS window lengths. */ struct rate_test { @@ -212,11 +207,10 @@ send_eos_event (GstElement * element) fail_unless (gst_pad_send_event (pad, event), "Cannot send EOS event: Not handled."); - /* There is no sink element, so _we_ post the EOS message on the bus - * here. Of course we generate any EOS ourselves, but this allows - * us to poll for the EOS message in poll_eos if we expect the - * element to _not_ generate a TAG message. That's better than - * waiting for a timeout to lapse. */ + /* There is no sink element, so _we_ post the EOS message on the bus here. Of + * course we generate any EOS ourselves, but this allows us to poll for the + * EOS message in poll_eos if we expect the element to _not_ generate a TAG + * message. That's better than waiting for a timeout to lapse. */ fail_unless (gst_bus_post (bus, gst_message_new_eos (NULL))); gst_object_unref (bus); @@ -251,8 +245,8 @@ poll_eos (GstElement * element) gst_object_unref (bus); } -/* This also polls for EOS since the TAG message comes right before - * the end of streams. */ +/* This also polls for EOS since the TAG message comes right before the end of + * streams. */ static GstTagList * poll_tags (GstElement * element) @@ -749,14 +743,13 @@ GST_END_TEST; /* Tests for correctness of the peak values. */ -/* Float peak test. For stereo, one channel has the constant value of - * -1.369, the other one 0.0. This tests many things: The result peak - * value should occur on any channel. The peak is of course the - * absolute amplitude, so 1.369 should be the result. This will also - * detect if the code uses the absolute value during the comparison. - * If it is buggy it will return 0.0 since 0.0 > -1.369. Furthermore, - * this makes sure that there is no problem with headroom (exceeding - * 0dBFS). In the wild you get float samples > 1.0 from stuff like +/* Float peak test. For stereo, one channel has the constant value of -1.369, + * the other one 0.0. This tests many things: The result peak value should + * occur on any channel. The peak is of course the absolute amplitude, so 1.369 + * should be the result. This will also detect if the code uses the absolute + * value during the comparison. If it is buggy it will return 0.0 since 0.0 > + * -1.369. Furthermore, this makes sure that there is no problem with headroom + * (exceeding 0dBFS). In the wild you get float samples > 1.0 from stuff like * vorbis. */ GST_START_TEST (test_peak_float) @@ -1089,11 +1082,10 @@ GST_START_TEST (test_peak_track_album) GST_END_TEST; -/* Disabling album processing before the end of the album. Probably a - * rare edge case and applications should not rely on this to work. - * They need to send the element to the READY state to clear up after - * an aborted album anyway since they might need to process another - * album afterwards. */ +/* Disabling album processing before the end of the album. Probably a rare edge + * case and applications should not rely on this to work. They need to send the + * element to the READY state to clear up after an aborted album anyway since + * they might need to process another album afterwards. */ GST_START_TEST (test_peak_album_abort_to_track) { @@ -1136,8 +1128,8 @@ GST_START_TEST (test_gain_album) g_object_set (element, "num-tracks", 3, NULL); set_playing_state (element); - /* The three tracks are constructed such that if any of these is in - * fact ignored for the album gain, the album gain will differ. */ + /* The three tracks are constructed such that if any of these is in fact + * ignored for the album gain, the album gain will differ. */ accumulator = 0; for (i = 8; i--;) @@ -1268,12 +1260,11 @@ GST_START_TEST (test_forced_separate) GST_END_TEST; -/* A TAG event is sent _after_ data has already been processed. In - * real pipelines, this could happen if there is more than one - * rganalysis element (by accident). While it would have analyzed all - * the data prior to receiving the event, I expect it to not post its - * results if not forced. This test is almost equivalent to - * test_forced. */ +/* A TAG event is sent _after_ data has already been processed. In real + * pipelines, this could happen if there is more than one rganalysis element (by + * accident). While it would have analyzed all the data prior to receiving the + * event, I expect it to not post its results if not forced. This test is + * almost equivalent to test_forced. */ GST_START_TEST (test_forced_after_data) { @@ -1311,8 +1302,8 @@ GST_START_TEST (test_forced_after_data) GST_END_TEST; -/* Like test_forced, but *analyze* an album afterwards. The two tests - * following this one check the *skipping* of albums. */ +/* Like test_forced, but *analyze* an album afterwards. The two tests following + * this one check the *skipping* of albums. */ GST_START_TEST (test_forced_album) { @@ -1441,9 +1432,8 @@ GST_START_TEST (test_forced_album_no_skip) gst_tag_list_free (tag_list); fail_unless_num_tracks (element, 1); - /* The second track has indeed full tags, but although being not - * forced, this one has to be processed because album processing is - * on. */ + /* The second track has indeed full tags, but although being not forced, this + * one has to be processed because album processing is on. */ tag_list = gst_tag_list_new (); /* Provided values are totally arbitrary. */ gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND, @@ -1515,10 +1505,10 @@ GST_START_TEST (test_reference_level) { GstElement *element = setup_rganalysis (); GstTagList *tag_list; + gdouble ref_level; gint accumulator = 0; gint i; - g_object_set (element, "reference-level", 83., "num-tracks", 2, NULL); set_playing_state (element); for (i = 20; i--;) @@ -1527,8 +1517,26 @@ GST_START_TEST (test_reference_level) send_eos_event (element); tag_list = poll_tags (element); fail_unless_track_peak (tag_list, 0.25); + fail_unless_track_gain (tag_list, get_expected_gain (44100)); + fail_if_album_tags (tag_list); + fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL, + &ref_level) && MATCH_GAIN (ref_level, 89.), + "Incorrect reference level tag"); + gst_tag_list_free (tag_list); + + g_object_set (element, "reference-level", 83., "num-tracks", 2, NULL); + + for (i = 20; i--;) + push_buffer (test_buffer_square_float_stereo (&accumulator, 44100, 512, + 0.25, 0.25)); + send_eos_event (element); + tag_list = poll_tags (element); + fail_unless_track_peak (tag_list, 0.25); fail_unless_track_gain (tag_list, get_expected_gain (44100) - 6.); fail_if_album_tags (tag_list); + fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL, + &ref_level) && MATCH_GAIN (ref_level, 83.), + "Incorrect reference level tag"); gst_tag_list_free (tag_list); accumulator = 0; @@ -1543,6 +1551,9 @@ GST_START_TEST (test_reference_level) /* We provided the same waveform twice, with a reset separating * them. Therefore, the album gain matches the track gain. */ fail_unless_album_gain (tag_list, get_expected_gain (44100) - 6.); + fail_unless (gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL, + &ref_level) && MATCH_GAIN (ref_level, 83.), + "Incorrect reference level tag"); gst_tag_list_free (tag_list); cleanup_rganalysis (element); diff --git a/tests/check/elements/rglimiter.c b/tests/check/elements/rglimiter.c new file mode 100644 index 00000000..2d4a715b --- /dev/null +++ b/tests/check/elements/rglimiter.c @@ -0,0 +1,238 @@ +/* GStreamer ReplayGain limiter + * + * Copyright (C) 2007 Rene Stadler <mail@renestadler.de> + * + * rglimiter.c: Unit test for the rglimiter element + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include <gst/check/gstcheck.h> + +#include <math.h> + +GList *buffers = NULL; + +/* For ease of programming we use globals to keep refs for our floating + * src and sink pads we create; otherwise we always have to do get_pad, + * get_peer, and then remove references in every test function */ +static GstPad *mysrcpad, *mysinkpad; + +#define RG_LIMITER_CAPS_TEMPLATE_STRING \ + "audio/x-raw-float, " \ + "width = (int) 32, " \ + "endianness = (int) BYTE_ORDER, " \ + "channels = (int) [ 1, MAX ], " \ + "rate = (int) [ 1, MAX ]" + +static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (RG_LIMITER_CAPS_TEMPLATE_STRING) + ); +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (RG_LIMITER_CAPS_TEMPLATE_STRING) + ); + +GstElement * +setup_rglimiter () +{ + GstElement *element; + GstBus *bus; + + GST_DEBUG ("setup_rglimiter"); + element = gst_check_setup_element ("rglimiter"); + mysrcpad = gst_check_setup_src_pad (element, &srctemplate, NULL); + mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate, NULL); + gst_pad_set_active (mysrcpad, TRUE); + gst_pad_set_active (mysinkpad, TRUE); + + return element; +} + +void +cleanup_rglimiter (GstElement * element) +{ + GST_DEBUG ("cleanup_rglimiter"); + + g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL); + g_list_free (buffers); + buffers = NULL; + + gst_check_teardown_src_pad (element); + gst_check_teardown_sink_pad (element); + gst_check_teardown_element (element); +} + +static void +set_playing_state (GstElement * element) +{ + fail_unless (gst_element_set_state (element, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "Could not set state to PLAYING"); +} + +static const gfloat test_input[] = { + -2.0, -1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0, 2.0 +}; +static const gfloat test_output[] = { + -0.99752737684336523, /* -2.0 */ + -0.88079707797788243, /* -1.0 */ + -0.7310585786300049, /* -0.75 */ + -0.5, -0.25, 0.0, 0.25, 0.5, + 0.7310585786300049, /* 0.75 */ + 0.88079707797788243, /* 1.0 */ + 0.99752737684336523, /* 2.0 */ +}; + +static GstBuffer * +create_test_buffer () +{ + GstBuffer *buf = gst_buffer_new_and_alloc (sizeof (test_input)); + GstCaps *caps; + + memcpy (GST_BUFFER_DATA (buf), test_input, sizeof (test_input)); + + caps = gst_caps_new_simple ("audio/x-raw-float", + "rate", G_TYPE_INT, 44100, "channels", G_TYPE_INT, 1, + "endianess", G_TYPE_INT, G_BYTE_ORDER, "width", G_TYPE_INT, 32, NULL); + gst_buffer_set_caps (buf, caps); + gst_caps_unref (caps); + + ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + + return buf; +} + +static void +verify_test_buffer (GstBuffer * buf) +{ + gfloat *output = (gfloat *) GST_BUFFER_DATA (buf); + gint i; + + fail_unless (GST_BUFFER_SIZE (buf) == sizeof (test_output)); + for (i = 0; i < G_N_ELEMENTS (test_input); i++) + fail_unless (ABS (output[i] - test_output[i]) < 1.e-6, + "Incorrect output value %.6f for input %.2f, expected %.6f", + output[i], test_input[i], test_output[i]); +} + +/* Start of tests. */ + +GST_START_TEST (test_no_buffer) +{ + GstElement *element = setup_rglimiter (); + + set_playing_state (element); + + cleanup_rglimiter (element); +} + +GST_END_TEST; + +GST_START_TEST (test_disabled) +{ + GstElement *element = setup_rglimiter (); + GstBuffer *buf, *out_buf; + + g_object_set (element, "enabled", FALSE, NULL); + set_playing_state (element); + + buf = create_test_buffer (); + fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK); + fail_unless (g_list_length (buffers) == 1); + out_buf = buffers->data; + fail_if (out_buf == NULL); + buffers = g_list_remove (buffers, out_buf); + ASSERT_BUFFER_REFCOUNT (out_buf, "out_buf", 1); + fail_unless (buf == out_buf); + gst_buffer_unref (out_buf); + + cleanup_rglimiter (element); +} + +GST_END_TEST; + +GST_START_TEST (test_limiting) +{ + GstElement *element = setup_rglimiter (); + GstBuffer *buf, *out_buf; + + set_playing_state (element); + + /* Mutable variant. */ + buf = create_test_buffer (); + fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK); + fail_unless (g_list_length (buffers) == 1); + out_buf = buffers->data; + fail_if (out_buf == NULL); + ASSERT_BUFFER_REFCOUNT (out_buf, "out_buf", 1); + verify_test_buffer (out_buf); + + /* Immutable variant. */ + buf = create_test_buffer (); + /* Extra ref: */ + gst_buffer_ref (buf); + ASSERT_BUFFER_REFCOUNT (buf, "buf", 2); + fail_unless (gst_pad_push (mysrcpad, buf) == GST_FLOW_OK); + ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + fail_unless (g_list_length (buffers) == 2); + out_buf = g_list_last (buffers)->data; + fail_if (out_buf == NULL); + ASSERT_BUFFER_REFCOUNT (out_buf, "out_buf", 1); + fail_unless (buf != out_buf); + /* Drop our extra ref: */ + gst_buffer_unref (buf); + verify_test_buffer (out_buf); + + cleanup_rglimiter (element); +} + +GST_END_TEST; + +Suite * +rglimiter_suite (void) +{ + Suite *s = suite_create ("rglimiter"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + + tcase_add_test (tc_chain, test_no_buffer); + tcase_add_test (tc_chain, test_disabled); + tcase_add_test (tc_chain, test_limiting); + + return s; +} + +int +main (int argc, char **argv) +{ + gint nf; + + Suite *s = rglimiter_suite (); + SRunner *sr = srunner_create (s); + + gst_check_init (&argc, &argv); + + srunner_run_all (sr, CK_ENV); + nf = srunner_ntests_failed (sr); + srunner_free (sr); + + return nf; +} diff --git a/tests/check/elements/rgvolume.c b/tests/check/elements/rgvolume.c new file mode 100644 index 00000000..658e98ce --- /dev/null +++ b/tests/check/elements/rgvolume.c @@ -0,0 +1,573 @@ +/* GStreamer ReplayGain volume adjustment + * + * Copyright (C) 2007 Rene Stadler <mail@renestadler.de> + * + * rgvolume.c: Unit test for the rgvolume element + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include <gst/check/gstcheck.h> + +#include <math.h> + +GList *buffers = NULL; +GList *events = NULL; + +/* For ease of programming we use globals to keep refs for our floating src and + * sink pads we create; otherwise we always have to do get_pad, get_peer, and + * then remove references in every test function */ +static GstPad *mysrcpad, *mysinkpad; + +#define RG_VOLUME_CAPS_TEMPLATE_STRING \ + "audio/x-raw-float, " \ + "width = (int) 32, " \ + "endianness = (int) BYTE_ORDER, " \ + "channels = (int) [ 1, MAX ], " \ + "rate = (int) [ 1, MAX ]" + +static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING) + ); +static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS (RG_VOLUME_CAPS_TEMPLATE_STRING) + ); + +/* gstcheck sets up a chain function that appends buffers to a global list. + * This is our equivalent of that for event handling. */ +static gboolean +event_func (GstPad * pad, GstEvent * event) +{ + events = g_list_append (events, event); + + return TRUE; +} + +GstElement * +setup_rgvolume () +{ + GstElement *element; + + GST_DEBUG ("setup_rgvolume"); + element = gst_check_setup_element ("rgvolume"); + mysrcpad = gst_check_setup_src_pad (element, &srctemplate, NULL); + mysinkpad = gst_check_setup_sink_pad (element, &sinktemplate, NULL); + + /* Capture events, to test tag filtering behavior: */ + gst_pad_set_event_function (mysinkpad, event_func); + + gst_pad_set_active (mysrcpad, TRUE); + gst_pad_set_active (mysinkpad, TRUE); + + return element; +} + +void +cleanup_rgvolume (GstElement * element) +{ + GST_DEBUG ("cleanup_rgvolume"); + + g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL); + g_list_free (buffers); + buffers = NULL; + + g_list_foreach (events, (GFunc) gst_mini_object_unref, NULL); + g_list_free (events); + events = NULL; + + gst_pad_set_active (mysrcpad, FALSE); + gst_pad_set_active (mysinkpad, FALSE); + gst_check_teardown_src_pad (element); + gst_check_teardown_sink_pad (element); + gst_check_teardown_element (element); +} + +static void +set_playing_state (GstElement * element) +{ + fail_unless (gst_element_set_state (element, + GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, + "Could not set state to PLAYING"); +} + +static void +set_null_state (GstElement * element) +{ + fail_unless (gst_element_set_state (element, + GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, + "Could not set state to NULL"); +} + +static void +send_eos_event (GstElement * element) +{ + GstEvent *event = gst_event_new_eos (); + + fail_unless (g_list_length (events) == 0); + fail_unless (gst_pad_push_event (mysrcpad, event), + "Pushing EOS event failed"); + fail_unless (g_list_length (events) == 1); + fail_unless (events->data == event); + gst_mini_object_unref ((GstMiniObject *) events->data); + events = g_list_remove (events, event); +} + +static GstEvent * +send_tag_event (GstElement * element, GstEvent * event) +{ + g_return_val_if_fail (event->type == GST_EVENT_TAG, NULL); + + fail_unless (g_list_length (events) == 0); + fail_unless (gst_pad_push_event (mysrcpad, event), + "Pushing tag event failed"); + + if (g_list_length (events) == 0) { + /* Event got filtered out. */ + event = NULL; + } else { + GstTagList *tag_list; + gdouble dummy; + + event = events->data; + events = g_list_remove (events, event); + + fail_unless (event->type == GST_EVENT_TAG); + gst_event_parse_tag (event, &tag_list); + + /* The element is supposed to filter out ReplayGain related tags. */ + fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN, &dummy), + "tag event still contains track gain tag"); + fail_if (gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK, &dummy), + "tag event still contains track peak tag"); + fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN, &dummy), + "tag event still contains album gain tag"); + fail_if (gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK, &dummy), + "tag event still contains album peak tag"); + } + + return event; +} + +static GstBuffer * +test_buffer_new (gfloat value) +{ + GstBuffer *buf; + GstCaps *caps; + gfloat *data; + gint i; + + buf = gst_buffer_new_and_alloc (8 * sizeof (gfloat)); + data = (gfloat *) GST_BUFFER_DATA (buf); + for (i = 0; i < 8; i++) + data[i] = value; + + caps = gst_caps_from_string ("audio/x-raw-float, " + "rate = 8000, channels = 1, endianess = BYTE_ORDER, width = 32"); + gst_buffer_set_caps (buf, caps); + gst_caps_unref (caps); + + ASSERT_BUFFER_REFCOUNT (buf, "buf", 1); + + return buf; +} + +#define MATCH_GAIN(g1, g2) ((g1 < g2 + 1e-6) && (g2 < g1 + 1e-6)) + +static void +fail_unless_target_gain (GstElement * element, gdouble expected_gain) +{ + gdouble prop_gain; + + g_object_get (element, "target-gain", &prop_gain, NULL); + + fail_unless (MATCH_GAIN (prop_gain, expected_gain), + "Target gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain); +} + +static void +fail_unless_result_gain (GstElement * element, gdouble expected_gain) +{ + GstBuffer *input_buf, *output_buf; + gfloat input_sample, output_sample; + gdouble gain, prop_gain; + gboolean is_passthrough, expect_passthrough; + gint i; + + fail_unless (g_list_length (buffers) == 0); + + input_sample = 1.0; + input_buf = test_buffer_new (input_sample); + + /* We keep an extra reference to detect passthrough mode. */ + gst_buffer_ref (input_buf); + /* Pushing steals a reference. */ + fail_unless (gst_pad_push (mysrcpad, input_buf) == GST_FLOW_OK); + gst_buffer_unref (input_buf); + + /* The output buffer ends up on the global buffer list. */ + fail_unless (g_list_length (buffers) == 1); + output_buf = buffers->data; + fail_if (output_buf == NULL); + + buffers = g_list_remove (buffers, output_buf); + ASSERT_BUFFER_REFCOUNT (output_buf, "output_buf", 1); + fail_unless_equals_int (GST_BUFFER_SIZE (output_buf), 8 * sizeof (gfloat)); + + output_sample = *((gfloat *) GST_BUFFER_DATA (output_buf)); + + fail_if (output_sample == 0.0, "First output sample is zero"); + for (i = 1; i < 8; i++) { + gfloat output = ((gfloat *) GST_BUFFER_DATA (output_buf))[i]; + + fail_unless (output_sample == output, "Output samples not uniform"); + }; + + gain = 20. * log10 (output_sample / input_sample); + fail_unless (MATCH_GAIN (gain, expected_gain), + "Applied gain is %.2f dB, expected %.2f dB", gain, expected_gain); + g_object_get (element, "result-gain", &prop_gain, NULL); + fail_unless (MATCH_GAIN (prop_gain, expected_gain), + "Result gain is %.2f dB, expected %.2f dB", prop_gain, expected_gain); + + is_passthrough = (output_buf == input_buf); + expect_passthrough = MATCH_GAIN (expected_gain, +0.00); + fail_unless (is_passthrough == expect_passthrough, + expect_passthrough + ? "Expected operation in passthrough mode" + : "Incorrect passthrough behaviour"); + + gst_buffer_unref (output_buf); +} + +static void +fail_unless_gain (GstElement * element, gdouble expected_gain) +{ + fail_unless_target_gain (element, expected_gain); + fail_unless_result_gain (element, expected_gain); +} + +/* Start of tests. */ + +GST_START_TEST (test_no_buffer) +{ + GstElement *element = setup_rgvolume (); + + set_playing_state (element); + set_null_state (element); + set_playing_state (element); + send_eos_event (element); + + cleanup_rgvolume (element); +} + +GST_END_TEST; + +GST_START_TEST (test_events) +{ + GstElement *element = setup_rgvolume (); + GstEvent *event; + GstEvent *new_event; + GstTagList *tag_list; + gchar *artist; + + set_playing_state (element); + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463, + GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415, + GST_TAG_ARTIST, "Foobar", NULL); + event = gst_event_new_tag (tag_list); + new_event = send_tag_event (element, event); + /* Expect the element to modify the writable event. */ + fail_unless (event == new_event, "Writable tag event not reused"); + gst_event_parse_tag (new_event, &tag_list); + fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist)); + fail_unless (g_str_equal (artist, "Foobar")); + g_free (artist); + gst_event_unref (new_event); + + /* Same as above, but with a non-writable event. */ + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, +4.95, GST_TAG_TRACK_PEAK, 0.59463, + GST_TAG_ALBUM_GAIN, -1.54, GST_TAG_ALBUM_PEAK, 0.693415, + GST_TAG_ARTIST, "Foobar", NULL); + event = gst_event_new_tag (tag_list); + /* Holding an extra ref makes the event unwritable: */ + gst_event_ref (event); + new_event = send_tag_event (element, event); + fail_unless (event != new_event, "Unwritable tag event reused"); + gst_event_parse_tag (new_event, &tag_list); + fail_unless (gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &artist)); + fail_unless (g_str_equal (artist, "Foobar")); + g_free (artist); + gst_event_unref (event); + gst_event_unref (new_event); + + cleanup_rgvolume (element); +} + +GST_END_TEST; + +GST_START_TEST (test_simple) +{ + GstElement *element = setup_rgvolume (); + GstTagList *tag_list; + + g_object_set (element, "album-mode", FALSE, "headroom", +0.00, + "pre-amp", -6.00, "fallback-gain", +1.23, NULL); + set_playing_state (element); + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0, + GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + fail_unless_gain (element, -9.45); /* pre-amp + track gain */ + send_eos_event (element); + + g_object_set (element, "album-mode", TRUE, NULL); + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, -3.45, GST_TAG_TRACK_PEAK, 1.0, + GST_TAG_ALBUM_GAIN, +2.09, GST_TAG_ALBUM_PEAK, 1.0, NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + fail_unless_gain (element, -3.91); /* pre-amp + album gain */ + + /* Switching back to track mode in the middle of a stream: */ + g_object_set (element, "album-mode", FALSE, NULL); + fail_unless_gain (element, -9.45); /* pre-amp + track gain */ + send_eos_event (element); + + cleanup_rgvolume (element); +} + +GST_END_TEST; + +/* If there are no gain tags at all, the fallback gain is used. */ + +GST_START_TEST (test_fallback_gain) +{ + GstElement *element = setup_rgvolume (); + GstTagList *tag_list; + + /* First some track where fallback does _not_ apply. */ + + g_object_set (element, "album-mode", FALSE, "headroom", 10.00, + "pre-amp", -6.00, "fallback-gain", -3.00, NULL); + set_playing_state (element); + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, +3.5, GST_TAG_TRACK_PEAK, 1.0, + GST_TAG_ALBUM_GAIN, -0.5, GST_TAG_ALBUM_PEAK, 1.0, NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + fail_unless_gain (element, -2.50); /* pre-amp + track gain */ + send_eos_event (element); + + /* Now a track completely missing tags. */ + + fail_unless_gain (element, -9.00); /* pre-amp + fallback-gain */ + + /* Changing the fallback gain in the middle of a stream, going to pass-through + * mode: */ + g_object_set (element, "fallback-gain", +6.00, NULL); + fail_unless_gain (element, +0.00); /* pre-amp + fallback-gain */ + send_eos_event (element); + + /* Verify that result gain is set to +0.00 with pre-amp + fallback-gain > + * +0.00 and no headroom. */ + + g_object_set (element, "fallback-gain", +12.00, "headroom", +0.00, NULL); + fail_unless_target_gain (element, +6.00); /* pre-amp + fallback-gain */ + fail_unless_result_gain (element, +0.00); + send_eos_event (element); + + cleanup_rgvolume (element); +} + +GST_END_TEST; + +/* If album gain is to be preferred but not available, the track gain is to be + * taken instead. */ + +GST_START_TEST (test_fallback_track) +{ + GstElement *element = setup_rgvolume (); + GstTagList *tag_list; + + g_object_set (element, "album-mode", TRUE, "headroom", +0.00, + "pre-amp", -6.00, "fallback-gain", +1.23, NULL); + set_playing_state (element); + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, +2.11, GST_TAG_TRACK_PEAK, 1.0, NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + fail_unless_gain (element, -3.89); /* pre-amp + track gain */ + + send_eos_event (element); + + cleanup_rgvolume (element); +} + +GST_END_TEST; + +/* If track gain is to be preferred but not available, the album gain is to be + * taken instead. */ + +GST_START_TEST (test_fallback_album) +{ + GstElement *element = setup_rgvolume (); + GstTagList *tag_list; + + g_object_set (element, "album-mode", FALSE, "headroom", +0.00, + "pre-amp", -6.00, "fallback-gain", +1.23, NULL); + set_playing_state (element); + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_ALBUM_GAIN, +3.73, GST_TAG_ALBUM_PEAK, 1.0, NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + fail_unless_gain (element, -2.27); /* pre-amp + album gain */ + + send_eos_event (element); + + cleanup_rgvolume (element); +} + +GST_END_TEST; + +GST_START_TEST (test_headroom) +{ + GstElement *element = setup_rgvolume (); + GstTagList *tag_list; + + g_object_set (element, "album-mode", FALSE, "headroom", +0.00, + "pre-amp", +0.00, "fallback-gain", +1.23, NULL); + set_playing_state (element); + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, +3.50, GST_TAG_TRACK_PEAK, 1.0, NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + fail_unless_target_gain (element, +3.50); /* pre-amp + track gain */ + fail_unless_result_gain (element, +0.00); + send_eos_event (element); + + g_object_set (element, "headroom", +2.00, NULL); + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, +9.18, GST_TAG_TRACK_PEAK, 0.687149, NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + fail_unless_target_gain (element, +9.18); /* pre-amp + track gain */ + /* Result is 20. * log10 (1. / peak) + headroom. */ + fail_unless_result_gain (element, 5.2589816238303335); + send_eos_event (element); + + g_object_set (element, "album-mode", TRUE, NULL); + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_ALBUM_GAIN, +5.50, GST_TAG_ALBUM_PEAK, 1.0, NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + fail_unless_target_gain (element, +5.50); /* pre-amp + album gain */ + fail_unless_result_gain (element, +2.00); /* headroom */ + send_eos_event (element); + + cleanup_rgvolume (element); +} + +GST_END_TEST; + +GST_START_TEST (test_reference_level) +{ + GstElement *element = setup_rgvolume (); + GstTagList *tag_list; + + g_object_set (element, + "album-mode", FALSE, + "headroom", +0.00, "pre-amp", +0.00, "fallback-gain", +1.23, NULL); + set_playing_state (element); + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, 0.00, GST_TAG_TRACK_PEAK, 0.2, + GST_TAG_REFERENCE_LEVEL, 83., NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + /* Because our authorative reference is 89 dB, we bump it up by +6 dB. */ + fail_unless_gain (element, +6.00); /* pre-amp + track gain */ + send_eos_event (element); + + g_object_set (element, "album-mode", TRUE, NULL); + + /* Same as above, but with album gain. */ + + tag_list = gst_tag_list_new (); + gst_tag_list_add (tag_list, GST_TAG_MERGE_REPLACE, + GST_TAG_TRACK_GAIN, 1.23, GST_TAG_TRACK_PEAK, 0.1, + GST_TAG_ALBUM_GAIN, 0.00, GST_TAG_ALBUM_PEAK, 0.2, + GST_TAG_REFERENCE_LEVEL, 83., NULL); + fail_unless (send_tag_event (element, gst_event_new_tag (tag_list)) == NULL); + fail_unless_gain (element, +6.00); /* pre-amp + album gain */ + + cleanup_rgvolume (element); +} + +GST_END_TEST; + +Suite * +rgvolume_suite (void) +{ + Suite *s = suite_create ("rgvolume"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + + tcase_add_test (tc_chain, test_no_buffer); + tcase_add_test (tc_chain, test_events); + tcase_add_test (tc_chain, test_simple); + tcase_add_test (tc_chain, test_fallback_gain); + tcase_add_test (tc_chain, test_fallback_track); + tcase_add_test (tc_chain, test_fallback_album); + tcase_add_test (tc_chain, test_headroom); + tcase_add_test (tc_chain, test_reference_level); + + return s; +} + +int +main (int argc, char **argv) +{ + gint nf; + + Suite *s = rgvolume_suite (); + SRunner *sr = srunner_create (s); + + gst_check_init (&argc, &argv); + + srunner_run_all (sr, CK_ENV); + nf = srunner_ntests_failed (sr); + srunner_free (sr); + + return nf; +} |