Debian Codegraph @

gst/replaygain/gstrgvolume.c

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