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