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