Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / replaygain / gstrgvolume.c
1 /* GStreamer ReplayGain volume adjustment
2  *
3  * Copyright (C) 2007 Rene Stadler <mail@renestadler.de>
4  * 
5  * gstrgvolume.c: Element to apply ReplayGain volume adjustment
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  * 
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  * 
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 /**
24  * SECTION:element-rgvolume
25  * @see_also: #GstRgLimiter, #GstRgAnalysis
26  *
27  * This element applies volume changes to streams as lined out in the proposed
28  * <ulink url="http://replaygain.org">ReplayGain standard</ulink>.  It
29  * interprets the ReplayGain meta data tags and carries out the adjustment (by
30  * using a volume element internally).  The relevant tags are:
31  * <itemizedlist>
32  * <listitem>#GST_TAG_TRACK_GAIN</listitem>
33  * <listitem>#GST_TAG_TRACK_PEAK</listitem>
34  * <listitem>#GST_TAG_ALBUM_GAIN</listitem>
35  * <listitem>#GST_TAG_ALBUM_PEAK</listitem>
36  * <listitem>#GST_TAG_REFERENCE_LEVEL</listitem>
37  * </itemizedlist>
38  * The information carried by these tags must have been calculated beforehand by
39  * performing the ReplayGain analysis.  This is implemented by the <link
40  * linkend="GstRgAnalysis">rganalysis</link> element.
41  * 
42  * The signal compression/limiting recommendations outlined in the proposed
43  * standard are not implemented by this element.  This has to be handled by
44  * separate elements because applications might want to have additional filters
45  * between the volume adjustment and the limiting stage.  A basic limiter is
46  * included with this plugin: The <link linkend="GstRgLimiter">rglimiter</link>
47  * element applies -6 dB hard limiting as mentioned in the ReplayGain standard.
48  * 
49  * <refsect2>
50  * <title>Example launch line</title>
51  * |[
52  * gst-launch filesrc location=filename.ext ! decodebin ! audioconvert \
53  *     ! rgvolume ! audioconvert ! audioresample ! alsasink
54  * ]| Playback of a file
55  * </refsect2>
56  */
57
58 #ifdef HAVE_CONFIG_H
59 #include <config.h>
60 #endif
61
62 #include <gst/gst.h>
63 #include <gst/pbutils/pbutils.h>
64 #include <math.h>
65
66 #include "gstrgvolume.h"
67 #include "replaygain.h"
68
69 GST_DEBUG_CATEGORY_STATIC (gst_rg_volume_debug);
70 #define GST_CAT_DEFAULT gst_rg_volume_debug
71
72 enum
73 {
74   PROP_0,
75   PROP_ALBUM_MODE,
76   PROP_HEADROOM,
77   PROP_PRE_AMP,
78   PROP_FALLBACK_GAIN,
79   PROP_TARGET_GAIN,
80   PROP_RESULT_GAIN
81 };
82
83 #define DEFAULT_ALBUM_MODE TRUE
84 #define DEFAULT_HEADROOM 0.0
85 #define DEFAULT_PRE_AMP 0.0
86 #define DEFAULT_FALLBACK_GAIN 0.0
87
88 #define DB_TO_LINEAR(x) pow (10., (x) / 20.)
89 #define LINEAR_TO_DB(x) (20. * log10 (x))
90
91 #define GAIN_FORMAT "+.02f dB"
92 #define PEAK_FORMAT ".06f"
93
94 #define VALID_GAIN(x) ((x) > -60.00 && (x) < 60.00)
95 #define VALID_PEAK(x) ((x) > 0.)
96
97 /* Same template caps as GstVolume, for I don't like having just ANY caps. */
98
99 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
100     GST_PAD_SINK, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, "
101         "rate = (int) [ 1, MAX ], "
102         "channels = (int) [ 1, MAX ], "
103         "endianness = (int) BYTE_ORDER, "
104         "width = (int) 32; "
105         "audio/x-raw-int, "
106         "channels = (int) [ 1, MAX ], "
107         "rate = (int) [ 1,  MAX ], "
108         "endianness = (int) BYTE_ORDER, "
109         "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE"));
110
111 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
112     GST_PAD_SRC, GST_PAD_ALWAYS, GST_STATIC_CAPS ("audio/x-raw-float, "
113         "rate = (int) [ 1, MAX ], "
114         "channels = (int) [ 1, MAX ], "
115         "endianness = (int) BYTE_ORDER, "
116         "width = (int) 32; "
117         "audio/x-raw-int, "
118         "channels = (int) [ 1, MAX ], "
119         "rate = (int) [ 1,  MAX ], "
120         "endianness = (int) BYTE_ORDER, "
121         "width = (int) 16, " "depth = (int) 16, " "signed = (bool) TRUE"));
122
123 GST_BOILERPLATE (GstRgVolume, gst_rg_volume, GstBin, GST_TYPE_BIN);
124
125 static void gst_rg_volume_set_property (GObject * object, guint prop_id,
126     const GValue * value, GParamSpec * pspec);
127 static void gst_rg_volume_get_property (GObject * object, guint prop_id,
128     GValue * value, GParamSpec * pspec);
129 static void gst_rg_volume_dispose (GObject * object);
130
131 static GstStateChangeReturn gst_rg_volume_change_state (GstElement * element,
132     GstStateChange transition);
133 static gboolean gst_rg_volume_sink_event (GstPad * pad, GstEvent * event);
134
135 static GstEvent *gst_rg_volume_tag_event (GstRgVolume * self, GstEvent * event);
136 static void gst_rg_volume_reset (GstRgVolume * self);
137 static void gst_rg_volume_update_gain (GstRgVolume * self);
138 static inline void gst_rg_volume_determine_gain (GstRgVolume * self,
139     gdouble * target_gain, gdouble * result_gain);
140
141 static void
142 gst_rg_volume_base_init (gpointer g_class)
143 {
144   GstElementClass *element_class = g_class;
145
146   gst_element_class_add_static_pad_template (element_class, &src_template);
147   gst_element_class_add_static_pad_template (element_class,
148       &sink_template);
149   gst_element_class_set_details_simple (element_class, "ReplayGain volume",
150       "Filter/Effect/Audio",
151       "Apply ReplayGain volume adjustment",
152       "Ren\xc3\xa9 Stadler <mail@renestadler.de>");
153
154   GST_DEBUG_CATEGORY_INIT (gst_rg_volume_debug, "rgvolume", 0,
155       "ReplayGain volume element");
156 }
157
158 static void
159 gst_rg_volume_class_init (GstRgVolumeClass * klass)
160 {
161   GObjectClass *gobject_class;
162   GstElementClass *element_class;
163   GstBinClass *bin_class;
164
165   gobject_class = (GObjectClass *) klass;
166
167   gobject_class->set_property = gst_rg_volume_set_property;
168   gobject_class->get_property = gst_rg_volume_get_property;
169   gobject_class->dispose = gst_rg_volume_dispose;
170
171   /**
172    * GstRgVolume:album-mode:
173    *
174    * Whether to prefer album gain over track gain.
175    *
176    * If set to %TRUE, use album gain instead of track gain if both are
177    * available.  This keeps the relative loudness levels of tracks from the same
178    * album intact.
179    *
180    * If set to %FALSE, track mode is used instead.  This effectively leads to
181    * more extensive normalization.
182    *
183    * If album mode is enabled but the album gain tag is absent in the stream,
184    * the track gain is used instead.  If both gain tags are missing, the value
185    * of the <link linkend="GstRgVolume--fallback-gain">fallback-gain</link>
186    * property is used instead.
187    */
188   g_object_class_install_property (gobject_class, PROP_ALBUM_MODE,
189       g_param_spec_boolean ("album-mode", "Album mode",
190           "Prefer album over track gain", DEFAULT_ALBUM_MODE,
191           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192   /**
193    * GstRgVolume:headroom:
194    *
195    * Extra headroom [dB].  This controls the amount by which the output can
196    * exceed digital full scale.
197    *
198    * Only set this to a value greater than 0.0 if signal compression/limiting of
199    * a suitable form is applied to the output (or output is brought into the
200    * correct range by some other transformation).
201    *
202    * This element internally uses a volume element, which also supports
203    * operating on integer audio formats.  These formats do not allow exceeding
204    * digital full scale.  If extra headroom is used, make sure that the raw
205    * audio data format is floating point (audio/x-raw-float).  Otherwise,
206    * clipping distortion might be introduced as part of the volume adjustment
207    * itself.
208    */
209   g_object_class_install_property (gobject_class, PROP_HEADROOM,
210       g_param_spec_double ("headroom", "Headroom", "Extra headroom [dB]",
211           0., 60., DEFAULT_HEADROOM,
212           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
213   /**
214    * GstRgVolume:pre-amp:
215    *
216    * Additional gain to apply globally [dB].  This controls the trade-off
217    * between uniformity of normalization and utilization of available dynamic
218    * range.
219    *
220    * Note that the default value is 0 dB because the ReplayGain reference value
221    * was adjusted by +6 dB (from 83 to 89 dB).  At the time of this writing, the
222    * <ulink url="http://replaygain.org">webpage</ulink> is still outdated and
223    * does not reflect this change however.  Where the original proposal states
224    * that a proper default pre-amp value is +6 dB, this translates to the used 0
225    * dB.
226    */
227   g_object_class_install_property (gobject_class, PROP_PRE_AMP,
228       g_param_spec_double ("pre-amp", "Pre-amp", "Extra gain [dB]",
229           -60., 60., DEFAULT_PRE_AMP,
230           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231   /**
232    * GstRgVolume:fallback-gain:
233    *
234    * Fallback gain [dB] for streams missing ReplayGain tags.
235    */
236   g_object_class_install_property (gobject_class, PROP_FALLBACK_GAIN,
237       g_param_spec_double ("fallback-gain", "Fallback gain",
238           "Gain for streams missing tags [dB]",
239           -60., 60., DEFAULT_FALLBACK_GAIN,
240           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241   /**
242    * GstRgVolume:result-gain:
243    *
244    * Applied gain [dB].  This gain is applied to processed buffer data.
245    *
246    * This is set to the <link linkend="GstRgVolume--target-gain">target
247    * gain</link> if amplification by that amount can be applied safely.
248    * "Safely" means that the volume adjustment does not inflict clipping
249    * distortion.  Should this not be the case, the result gain is set to an
250    * appropriately reduced value (by applying peak normalization).  The proposed
251    * standard calls this "clipping prevention".
252    *
253    * The difference between target and result gain reflects the necessary amount
254    * of reduction.  Applications can make use of this information to temporarily
255    * reduce the <link linkend="GstRgVolume--pre-amp">pre-amp</link> for
256    * subsequent streams, as recommended by the ReplayGain standard.
257    *
258    * Note that target and result gain differing for a great majority of streams
259    * indicates a problem: What happens in this case is that most streams receive
260    * peak normalization instead of amplification by the ideal replay gain.  To
261    * prevent this, the <link linkend="GstRgVolume--pre-amp">pre-amp</link> has
262    * to be lowered and/or a limiter has to be used which facilitates the use of
263    * <link linkend="GstRgVolume--headroom">headroom</link>.
264    */
265   g_object_class_install_property (gobject_class, PROP_RESULT_GAIN,
266       g_param_spec_double ("result-gain", "Result-gain", "Applied gain [dB]",
267           -120., 120., 0., G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
268   /**
269    * GstRgVolume:target-gain:
270    *
271    * Applicable gain [dB].  This gain is supposed to be applied.
272    *
273    * Depending on the value of the <link
274    * linkend="GstRgVolume--album-mode">album-mode</link> property and the
275    * presence of ReplayGain tags in the stream, this is set according to one of
276    * these simple formulas:
277    *
278    * <itemizedlist>
279    * <listitem><link linkend="GstRgVolume--pre-amp">pre-amp</link> + album gain
280    * of the stream</listitem>
281    * <listitem><link linkend="GstRgVolume--pre-amp">pre-amp</link> + track gain
282    * of the stream</listitem>
283    * <listitem><link linkend="GstRgVolume--pre-amp">pre-amp</link> + <link
284    * linkend="GstRgVolume--fallback-gain">fallback gain</link></listitem>
285    * </itemizedlist>
286    */
287   g_object_class_install_property (gobject_class, PROP_TARGET_GAIN,
288       g_param_spec_double ("target-gain", "Target-gain",
289           "Applicable gain [dB]", -120., 120., 0.,
290           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
291
292   element_class = (GstElementClass *) klass;
293   element_class->change_state = GST_DEBUG_FUNCPTR (gst_rg_volume_change_state);
294
295   bin_class = (GstBinClass *) klass;
296   /* Setting these to NULL makes gst_bin_add and _remove refuse to let anyone
297    * mess with our internals. */
298   bin_class->add_element = NULL;
299   bin_class->remove_element = NULL;
300 }
301
302 static void
303 gst_rg_volume_init (GstRgVolume * self, GstRgVolumeClass * gclass)
304 {
305   GObjectClass *volume_class;
306   GstPad *volume_pad, *ghost_pad;
307
308   self->album_mode = DEFAULT_ALBUM_MODE;
309   self->headroom = DEFAULT_HEADROOM;
310   self->pre_amp = DEFAULT_PRE_AMP;
311   self->fallback_gain = DEFAULT_FALLBACK_GAIN;
312   self->target_gain = 0.0;
313   self->result_gain = 0.0;
314
315   self->volume_element = gst_element_factory_make ("volume", "rgvolume-volume");
316   if (G_UNLIKELY (self->volume_element == NULL)) {
317     GstMessage *msg;
318
319     GST_WARNING_OBJECT (self, "could not create volume element");
320     msg = gst_missing_element_message_new (GST_ELEMENT_CAST (self), "volume");
321     gst_element_post_message (GST_ELEMENT_CAST (self), msg);
322
323     /* Nothing else to do, we will refuse the state change from NULL to READY to
324      * indicate that something went very wrong.  It is doubtful that someone
325      * attempts changing our state though, since we end up having no pads! */
326     return;
327   }
328
329   volume_class = G_OBJECT_GET_CLASS (G_OBJECT (self->volume_element));
330   self->max_volume = G_PARAM_SPEC_DOUBLE
331       (g_object_class_find_property (volume_class, "volume"))->maximum;
332
333   GST_BIN_CLASS (parent_class)->add_element (GST_BIN_CAST (self),
334       self->volume_element);
335
336   volume_pad = gst_element_get_static_pad (self->volume_element, "sink");
337   ghost_pad = gst_ghost_pad_new_from_template ("sink", volume_pad,
338       gst_pad_get_pad_template (volume_pad));
339   gst_object_unref (volume_pad);
340   gst_pad_set_event_function (ghost_pad, gst_rg_volume_sink_event);
341   gst_element_add_pad (GST_ELEMENT_CAST (self), ghost_pad);
342
343   volume_pad = gst_element_get_static_pad (self->volume_element, "src");
344   ghost_pad = gst_ghost_pad_new_from_template ("src", volume_pad,
345       gst_pad_get_pad_template (volume_pad));
346   gst_object_unref (volume_pad);
347   gst_element_add_pad (GST_ELEMENT_CAST (self), ghost_pad);
348 }
349
350 static void
351 gst_rg_volume_set_property (GObject * object, guint prop_id,
352     const GValue * value, GParamSpec * pspec)
353 {
354   GstRgVolume *self = GST_RG_VOLUME (object);
355
356   switch (prop_id) {
357     case PROP_ALBUM_MODE:
358       self->album_mode = g_value_get_boolean (value);
359       break;
360     case PROP_HEADROOM:
361       self->headroom = g_value_get_double (value);
362       break;
363     case PROP_PRE_AMP:
364       self->pre_amp = g_value_get_double (value);
365       break;
366     case PROP_FALLBACK_GAIN:
367       self->fallback_gain = g_value_get_double (value);
368       break;
369     default:
370       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371       break;
372   }
373
374   gst_rg_volume_update_gain (self);
375 }
376
377 static void
378 gst_rg_volume_get_property (GObject * object, guint prop_id,
379     GValue * value, GParamSpec * pspec)
380 {
381   GstRgVolume *self = GST_RG_VOLUME (object);
382
383   switch (prop_id) {
384     case PROP_ALBUM_MODE:
385       g_value_set_boolean (value, self->album_mode);
386       break;
387     case PROP_HEADROOM:
388       g_value_set_double (value, self->headroom);
389       break;
390     case PROP_PRE_AMP:
391       g_value_set_double (value, self->pre_amp);
392       break;
393     case PROP_FALLBACK_GAIN:
394       g_value_set_double (value, self->fallback_gain);
395       break;
396     case PROP_TARGET_GAIN:
397       g_value_set_double (value, self->target_gain);
398       break;
399     case PROP_RESULT_GAIN:
400       g_value_set_double (value, self->result_gain);
401       break;
402     default:
403       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
404       break;
405   }
406 }
407
408 static void
409 gst_rg_volume_dispose (GObject * object)
410 {
411   GstRgVolume *self = GST_RG_VOLUME (object);
412
413   if (self->volume_element != NULL) {
414     /* Manually remove our child using the bin implementation of remove_element.
415      * This is needed because we prevent gst_bin_remove from working, which the
416      * parent dispose handler would use if we had any children left. */
417     GST_BIN_CLASS (parent_class)->remove_element (GST_BIN_CAST (self),
418         self->volume_element);
419     self->volume_element = NULL;
420   }
421
422   G_OBJECT_CLASS (parent_class)->dispose (object);
423 }
424
425 static GstStateChangeReturn
426 gst_rg_volume_change_state (GstElement * element, GstStateChange transition)
427 {
428   GstRgVolume *self = GST_RG_VOLUME (element);
429   GstStateChangeReturn res;
430
431   switch (transition) {
432     case GST_STATE_CHANGE_NULL_TO_READY:
433
434       if (G_UNLIKELY (self->volume_element == NULL)) {
435         /* Creating our child volume element in _init failed. */
436         return GST_STATE_CHANGE_FAILURE;
437       }
438       break;
439
440     case GST_STATE_CHANGE_READY_TO_PAUSED:
441
442       gst_rg_volume_reset (self);
443       break;
444
445     default:
446       break;
447   }
448
449   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
450
451   return res;
452 }
453
454 /* Event function for the ghost sink pad. */
455 static gboolean
456 gst_rg_volume_sink_event (GstPad * pad, GstEvent * event)
457 {
458   GstRgVolume *self;
459   GstPad *volume_sink_pad;
460   GstEvent *send_event = event;
461   gboolean res;
462
463   self = GST_RG_VOLUME (gst_pad_get_parent_element (pad));
464   volume_sink_pad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
465
466   switch (GST_EVENT_TYPE (event)) {
467     case GST_EVENT_TAG:
468
469       GST_LOG_OBJECT (self, "received tag event");
470
471       send_event = gst_rg_volume_tag_event (self, event);
472
473       if (send_event == NULL)
474         GST_LOG_OBJECT (self, "all tags handled, dropping event");
475
476       break;
477
478     case GST_EVENT_EOS:
479
480       gst_rg_volume_reset (self);
481       break;
482
483     default:
484       break;
485   }
486
487   if (G_LIKELY (send_event != NULL))
488     res = gst_pad_send_event (volume_sink_pad, send_event);
489   else
490     res = TRUE;
491
492   gst_object_unref (volume_sink_pad);
493   gst_object_unref (self);
494   return res;
495 }
496
497 static GstEvent *
498 gst_rg_volume_tag_event (GstRgVolume * self, GstEvent * event)
499 {
500   GstTagList *tag_list;
501   gboolean has_track_gain, has_track_peak, has_album_gain, has_album_peak;
502   gboolean has_ref_level;
503
504   g_return_val_if_fail (event != NULL, NULL);
505   g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TAG, event);
506
507   gst_event_parse_tag (event, &tag_list);
508
509   if (gst_tag_list_is_empty (tag_list))
510     return event;
511
512   has_track_gain = gst_tag_list_get_double (tag_list, GST_TAG_TRACK_GAIN,
513       &self->track_gain);
514   has_track_peak = gst_tag_list_get_double (tag_list, GST_TAG_TRACK_PEAK,
515       &self->track_peak);
516   has_album_gain = gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_GAIN,
517       &self->album_gain);
518   has_album_peak = gst_tag_list_get_double (tag_list, GST_TAG_ALBUM_PEAK,
519       &self->album_peak);
520   has_ref_level = gst_tag_list_get_double (tag_list, GST_TAG_REFERENCE_LEVEL,
521       &self->reference_level);
522
523   if (!has_track_gain && !has_track_peak && !has_album_gain && !has_album_peak)
524     return event;
525
526   if (has_ref_level && (has_track_gain || has_album_gain)
527       && (ABS (self->reference_level - RG_REFERENCE_LEVEL) > 1.e-6)) {
528     /* Log a message stating the amount of adjustment that is applied below. */
529     GST_DEBUG_OBJECT (self,
530         "compensating for reference level difference by %" GAIN_FORMAT,
531         RG_REFERENCE_LEVEL - self->reference_level);
532   }
533   if (has_track_gain) {
534     self->track_gain += RG_REFERENCE_LEVEL - self->reference_level;
535   }
536   if (has_album_gain) {
537     self->album_gain += RG_REFERENCE_LEVEL - self->reference_level;
538   }
539
540   /* Ignore values that are obviously invalid. */
541   if (G_UNLIKELY (has_track_gain && !VALID_GAIN (self->track_gain))) {
542     GST_DEBUG_OBJECT (self,
543         "ignoring bogus track gain value %" GAIN_FORMAT, self->track_gain);
544     has_track_gain = FALSE;
545   }
546   if (G_UNLIKELY (has_track_peak && !VALID_PEAK (self->track_peak))) {
547     GST_DEBUG_OBJECT (self,
548         "ignoring bogus track peak value %" PEAK_FORMAT, self->track_peak);
549     has_track_peak = FALSE;
550   }
551   if (G_UNLIKELY (has_album_gain && !VALID_GAIN (self->album_gain))) {
552     GST_DEBUG_OBJECT (self,
553         "ignoring bogus album gain value %" GAIN_FORMAT, self->album_gain);
554     has_album_gain = FALSE;
555   }
556   if (G_UNLIKELY (has_album_peak && !VALID_PEAK (self->album_peak))) {
557     GST_DEBUG_OBJECT (self,
558         "ignoring bogus album peak value %" PEAK_FORMAT, self->album_peak);
559     has_album_peak = FALSE;
560   }
561
562   /* Clamp peaks >1.0.  Float based decoders can produce spurious samples >1.0,
563    * cutting these files back to 1.0 should not cause any audible distortion.
564    * This is most often seen with Vorbis files. */
565   if (has_track_peak && self->track_peak > 1.) {
566     GST_DEBUG_OBJECT (self,
567         "clamping track peak %" PEAK_FORMAT " to 1.0", self->track_peak);
568     self->track_peak = 1.0;
569   }
570   if (has_album_peak && self->album_peak > 1.) {
571     GST_DEBUG_OBJECT (self,
572         "clamping album peak %" PEAK_FORMAT " to 1.0", self->album_peak);
573     self->album_peak = 1.0;
574   }
575
576   self->has_track_gain |= has_track_gain;
577   self->has_track_peak |= has_track_peak;
578   self->has_album_gain |= has_album_gain;
579   self->has_album_peak |= has_album_peak;
580
581   event = (GstEvent *) gst_mini_object_make_writable (GST_MINI_OBJECT (event));
582   gst_event_parse_tag (event, &tag_list);
583
584   gst_tag_list_remove_tag (tag_list, GST_TAG_TRACK_GAIN);
585   gst_tag_list_remove_tag (tag_list, GST_TAG_TRACK_PEAK);
586   gst_tag_list_remove_tag (tag_list, GST_TAG_ALBUM_GAIN);
587   gst_tag_list_remove_tag (tag_list, GST_TAG_ALBUM_PEAK);
588   gst_tag_list_remove_tag (tag_list, GST_TAG_REFERENCE_LEVEL);
589
590   gst_rg_volume_update_gain (self);
591
592   if (gst_tag_list_is_empty (tag_list)) {
593     gst_event_unref (event);
594     event = NULL;
595   }
596
597   return event;
598 }
599
600 static void
601 gst_rg_volume_reset (GstRgVolume * self)
602 {
603   self->has_track_gain = FALSE;
604   self->has_track_peak = FALSE;
605   self->has_album_gain = FALSE;
606   self->has_album_peak = FALSE;
607
608   self->reference_level = RG_REFERENCE_LEVEL;
609
610   gst_rg_volume_update_gain (self);
611 }
612
613 static void
614 gst_rg_volume_update_gain (GstRgVolume * self)
615 {
616   gdouble target_gain, result_gain, result_volume;
617   gboolean target_gain_changed, result_gain_changed;
618
619   gst_rg_volume_determine_gain (self, &target_gain, &result_gain);
620
621   result_volume = DB_TO_LINEAR (result_gain);
622
623   /* Ensure that the result volume is within the range that the volume element
624    * can handle.  Currently, the limit is 10. (+20 dB), which should not be
625    * restrictive. */
626   if (G_UNLIKELY (result_volume > self->max_volume)) {
627     GST_INFO_OBJECT (self,
628         "cannot handle result gain of %" GAIN_FORMAT " (%0.6f), adjusting",
629         result_gain, result_volume);
630
631     result_volume = self->max_volume;
632     result_gain = LINEAR_TO_DB (result_volume);
633   }
634
635   /* Direct comparison is OK in this case. */
636   if (target_gain == result_gain) {
637     GST_INFO_OBJECT (self,
638         "result gain is %" GAIN_FORMAT " (%0.6f), matching target",
639         result_gain, result_volume);
640   } else {
641     GST_INFO_OBJECT (self,
642         "result gain is %" GAIN_FORMAT " (%0.6f), target is %" GAIN_FORMAT,
643         result_gain, result_volume, target_gain);
644   }
645
646   target_gain_changed = (self->target_gain != target_gain);
647   result_gain_changed = (self->result_gain != result_gain);
648
649   self->target_gain = target_gain;
650   self->result_gain = result_gain;
651
652   g_object_set (self->volume_element, "volume", result_volume, NULL);
653
654   if (target_gain_changed)
655     g_object_notify ((GObject *) self, "target-gain");
656   if (result_gain_changed)
657     g_object_notify ((GObject *) self, "result-gain");
658 }
659
660 static inline void
661 gst_rg_volume_determine_gain (GstRgVolume * self, gdouble * target_gain,
662     gdouble * result_gain)
663 {
664   gdouble gain, peak;
665
666   if (!self->has_track_gain && !self->has_album_gain) {
667
668     GST_DEBUG_OBJECT (self, "using fallback gain");
669     gain = self->fallback_gain;
670     peak = 1.0;
671
672   } else if ((self->album_mode && self->has_album_gain)
673       || (!self->album_mode && !self->has_track_gain)) {
674
675     gain = self->album_gain;
676     if (G_LIKELY (self->has_album_peak)) {
677       peak = self->album_peak;
678     } else {
679       GST_DEBUG_OBJECT (self, "album peak missing, assuming 1.0");
680       peak = 1.0;
681     }
682     /* Falling back from track to album gain shouldn't really happen. */
683     if (G_UNLIKELY (!self->album_mode))
684       GST_INFO_OBJECT (self, "falling back to album gain");
685
686   } else {
687     /* !album_mode && !has_album_gain || album_mode && has_track_gain */
688
689     gain = self->track_gain;
690     if (G_LIKELY (self->has_track_peak)) {
691       peak = self->track_peak;
692     } else {
693       GST_DEBUG_OBJECT (self, "track peak missing, assuming 1.0");
694       peak = 1.0;
695     }
696     if (self->album_mode)
697       GST_INFO_OBJECT (self, "falling back to track gain");
698   }
699
700   gain += self->pre_amp;
701
702   *target_gain = gain;
703   *result_gain = gain;
704
705   if (LINEAR_TO_DB (peak) + gain > self->headroom) {
706     *result_gain = LINEAR_TO_DB (1. / peak) + self->headroom;
707   }
708 }