1 /* GStreamer ReplayGain analysis
2 *
3 * Copyright (C) 2006 Rene Stadler <mail@renestadler.de>
4 *
5 * gstrganalysis.c: Element that performs the ReplayGain analysis
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-rganalysis
25 * @title: rganalysis
26 * @see_also: #GstRgVolume
27 *
28 * This element analyzes raw audio sample data in accordance with the proposed
29 * [ReplayGain standard](https://wiki.hydrogenaud.io/index.php?title=ReplayGain) for
30 * calculating the ideal replay gain for music tracks and albums. The element
31 * is designed as a pass-through filter that never modifies any data. As it
32 * receives an EOS event, it finalizes the ongoing analysis and generates a tag
33 * list containing the results. It is sent downstream with a tag event and
34 * posted on the message bus with a tag message. The EOS event is forwarded as
35 * normal afterwards. Result tag lists at least contain the tags
36 * #GST_TAG_TRACK_GAIN, #GST_TAG_TRACK_PEAK and #GST_TAG_REFERENCE_LEVEL.
37 *
38 * Because the generated metadata tags become available at the end of streams,
39 * downstream muxer and encoder elements are normally unable to save them in
40 * their output since they generally save metadata in the file header.
41 * Therefore, it is often necessary that applications read the results in a bus
42 * event handler for the tag message. Obtaining the values this way is always
43 * needed for album processing (see #GstRgAnalysis:num-tracks property) since
44 * the album gain and peak values need to be associated with all tracks of an
45 * album, not just the last one.
46 *
47 * ## Example launch lines
48 * |[
49 * gst-launch-1.0 -t audiotestsrc wave=sine num-buffers=512 ! rganalysis ! fakesink
50 * ]| Analyze a simple test waveform
51 * |[
52 * gst-launch-1.0 -t filesrc location=filename.ext ! decodebin \
53 * ! audioconvert ! audioresample ! rganalysis ! fakesink
54 * ]| Analyze a given file
55 * |[
56 * gst-launch-1.0 -t gnomevfssrc location=http://replaygain.hydrogenaudio.org/ref_pink.wav \
57 * ! wavparse ! rganalysis ! fakesink
58 * ]| Analyze the pink noise reference file
59 *
60 * The above launch line yields a result gain of +6 dB (instead of the expected
61 * +0 dB). This is not in error, refer to the #GstRgAnalysis:reference-level
62 * property documentation for more information.
63 *
64 * ## Acknowledgements
65 *
66 * This element is based on code used in the [vorbisgain](https://sjeng.org/vorbisgain.html)
67 * program and many others. The relevant parts are copyrighted by David Robinson, Glen Sawyer
68 * and Frank Klemm.
69 *
70 */
71
72 #ifdef HAVE_CONFIG_H
73 #include <config.h>
74 #endif
75
76 #include <gst/gst.h>
77 #include <gst/base/gstbasetransform.h>
78 #include <gst/audio/audio.h>
79
80 #include "gstrganalysis.h"
81 #include "replaygain.h"
82
83 GST_DEBUG_CATEGORY_STATIC (gst_rg_analysis_debug);
84 #define GST_CAT_DEFAULT gst_rg_analysis_debug
85
86 /* Default property value. */
87 #define FORCED_DEFAULT TRUE
88 #define DEFAULT_MESSAGE FALSE
89
90 enum
91 {
92 PROP_0,
93 PROP_NUM_TRACKS,
94 PROP_FORCED,
95 PROP_REFERENCE_LEVEL,
96 PROP_MESSAGE
97 };
98
99 /* The ReplayGain algorithm is intended for use with mono and stereo
100 * audio. The used implementation has filter coefficients for the
101 * "usual" sample rates in the 8000 to 48000 Hz range. */
102 #define REPLAY_GAIN_CAPS "audio/x-raw," \
103 "format = (string) { "GST_AUDIO_NE(F32)","GST_AUDIO_NE(S16)" }, " \
104 "layout = (string) interleaved, " \
105 "channels = (int) 1, " \
106 "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, " \
107 "44100, 48000 }; " \
108 "audio/x-raw," \
109 "format = (string) { "GST_AUDIO_NE(F32)","GST_AUDIO_NE(S16)" }, " \
110 "layout = (string) interleaved, " \
111 "channels = (int) 2, " \
112 "channel-mask = (bitmask) 0x3, " \
113 "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, " \
114 "44100, 48000 }"
115
116 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
117 GST_PAD_SINK,
118 GST_PAD_ALWAYS,
119 GST_STATIC_CAPS (REPLAY_GAIN_CAPS));
120
121 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
122 GST_PAD_SRC,
123 GST_PAD_ALWAYS,
124 GST_STATIC_CAPS (REPLAY_GAIN_CAPS));
125
126 #define gst_rg_analysis_parent_class parent_class
127 G_DEFINE_TYPE (GstRgAnalysis, gst_rg_analysis, GST_TYPE_BASE_TRANSFORM);
128 GST_ELEMENT_REGISTER_DEFINE (rganalysis, "rganalysis", GST_RANK_NONE,
129 GST_TYPE_RG_ANALYSIS);
130
131 static void gst_rg_analysis_set_property (GObject * object, guint prop_id,
132 const GValue * value, GParamSpec * pspec);
133 static void gst_rg_analysis_get_property (GObject * object, guint prop_id,
134 GValue * value, GParamSpec * pspec);
135
136 static gboolean gst_rg_analysis_start (GstBaseTransform * base);
137 static gboolean gst_rg_analysis_set_caps (GstBaseTransform * base,
138 GstCaps * incaps, GstCaps * outcaps);
139 static GstFlowReturn gst_rg_analysis_transform_ip (GstBaseTransform * base,
140 GstBuffer * buf);
141 static gboolean gst_rg_analysis_sink_event (GstBaseTransform * base,
142 GstEvent * event);
143 static gboolean gst_rg_analysis_stop (GstBaseTransform * base);
144
145 static void gst_rg_analysis_handle_tags (GstRgAnalysis * filter,
146 const GstTagList * tag_list);
147 static void gst_rg_analysis_handle_eos (GstRgAnalysis * filter);
148 static gboolean gst_rg_analysis_track_result (GstRgAnalysis * filter,
149 GstTagList ** tag_list);
150 static gboolean gst_rg_analysis_album_result (GstRgAnalysis * filter,
151 GstTagList ** tag_list);
152
153 static void
gst_rg_analysis_class_init(GstRgAnalysisClass * klass)154 gst_rg_analysis_class_init (GstRgAnalysisClass * klass)
155 {
156 GObjectClass *gobject_class;
157 GstElementClass *element_class;
158 GstBaseTransformClass *trans_class;
159
160 gobject_class = (GObjectClass *) klass;
161 element_class = (GstElementClass *) klass;
162
163 gobject_class->set_property = gst_rg_analysis_set_property;
164 gobject_class->get_property = gst_rg_analysis_get_property;
165
166 /**
167 * GstRgAnalysis:num-tracks:
168 *
169 * Number of remaining album tracks.
170 *
171 * Analyzing several streams sequentially and assigning them a common result
172 * gain is known as "album processing". If this gain is used during playback
173 * (by switching to "album mode"), all tracks of an album receive the same
174 * amplification. This keeps the relative volume levels between the tracks
175 * intact. To enable this, set this property to the number of streams that
176 * will be processed as album tracks.
177 *
178 * Every time an EOS event is received, the value of this property is
179 * decremented by one. As it reaches zero, it is assumed that the last track
180 * of the album finished. The tag list for the final stream will contain the
181 * additional tags #GST_TAG_ALBUM_GAIN and #GST_TAG_ALBUM_PEAK. All other
182 * streams just get the two track tags posted because the values for the album
183 * tags are not known before all tracks are analyzed. Applications need to
184 * ensure that the album gain and peak values are also associated with the
185 * other tracks when storing the results.
186 *
187 * If the total number of album tracks is unknown beforehand, just ensure that
188 * the value is greater than 1 before each track starts. Then before the end
189 * of the last track, set it to the value 1.
190 *
191 * To perform album processing, the element has to preserve data between
192 * streams. This cannot survive a state change to the NULL or READY state.
193 * If you change your pipeline's state to NULL or READY between tracks, lock
194 * the element's state using gst_element_set_locked_state() when it is in
195 * PAUSED or PLAYING.
196 */
197 g_object_class_install_property (gobject_class, PROP_NUM_TRACKS,
198 g_param_spec_int ("num-tracks", "Number of album tracks",
199 "Number of remaining album tracks", 0, G_MAXINT, 0,
200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201 /**
202 * GstRgAnalysis:forced:
203 *
204 * Whether to analyze streams even when ReplayGain tags exist.
205 *
206 * For assisting transcoder/converter applications, the element can silently
207 * skip the processing of streams that already contain the necessary tags.
208 * Data will flow as usual but the element will not consume CPU time and will
209 * not generate result tags. To enable possible skipping, set this property
210 * to %FALSE.
211 *
212 * If used in conjunction with <link linkend="GstRgAnalysis--num-tracks">album
213 * processing</link>, the element will skip the number of remaining album
214 * tracks if a full set of tags is found for the first track. If a subsequent
215 * track of the album is missing tags, processing cannot start again. If this
216 * is undesired, the application has to scan all files beforehand and enable
217 * forcing of processing if needed.
218 */
219 g_object_class_install_property (gobject_class, PROP_FORCED,
220 g_param_spec_boolean ("forced", "Forced",
221 "Analyze even if ReplayGain tags exist",
222 FORCED_DEFAULT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 /**
224 * GstRgAnalysis:reference-level:
225 *
226 * Reference level [dB].
227 *
228 * Analyzing the ReplayGain pink noise reference waveform computes a result of
229 * +6 dB instead of the expected 0 dB. This is because the default reference
230 * level is 89 dB. To obtain values as lined out in the original proposal of
231 * ReplayGain, set this property to 83.
232 *
233 * Almost all software uses 89 dB as a reference however, and this value has
234 * become the new official value, and that change has been acclaimed by the
235 * original author of the ReplayGain proposal.
236 *
237 * The value was changed because the original proposal recommends a default
238 * pre-amp value of +6 dB for playback. This seemed a bit odd, as it means
239 * that the algorithm has the general tendency to produce adjustment values
240 * that are 6 dB too low. Bumping the reference level by 6 dB compensated for
241 * this.
242 *
243 * The problem of the reference level being ambiguous for lack of concise
244 * standardization is to be solved by adopting the #GST_TAG_REFERENCE_LEVEL
245 * tag, which allows to store the used value alongside the gain values.
246 */
247 g_object_class_install_property (gobject_class, PROP_REFERENCE_LEVEL,
248 g_param_spec_double ("reference-level", "Reference level",
249 "Reference level [dB]", 0.0, 150., RG_REFERENCE_LEVEL,
250 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
251
252 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MESSAGE,
253 g_param_spec_boolean ("message", "Message",
254 "Post statics messages",
255 DEFAULT_MESSAGE,
256 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
257
258 trans_class = (GstBaseTransformClass *) klass;
259 trans_class->start = GST_DEBUG_FUNCPTR (gst_rg_analysis_start);
260 trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_rg_analysis_set_caps);
261 trans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_rg_analysis_transform_ip);
262 trans_class->sink_event = GST_DEBUG_FUNCPTR (gst_rg_analysis_sink_event);
263 trans_class->stop = GST_DEBUG_FUNCPTR (gst_rg_analysis_stop);
264 trans_class->passthrough_on_same_caps = TRUE;
265
266 gst_element_class_add_static_pad_template (element_class, &src_factory);
267 gst_element_class_add_static_pad_template (element_class, &sink_factory);
268 gst_element_class_set_static_metadata (element_class, "ReplayGain analysis",
269 "Filter/Analyzer/Audio",
270 "Perform the ReplayGain analysis",
271 "Ren\xc3\xa9 Stadler <mail@renestadler.de>");
272
273 GST_DEBUG_CATEGORY_INIT (gst_rg_analysis_debug, "rganalysis", 0,
274 "ReplayGain analysis element");
275 }
276
277 static void
gst_rg_analysis_init(GstRgAnalysis * filter)278 gst_rg_analysis_init (GstRgAnalysis * filter)
279 {
280 GstBaseTransform *base = GST_BASE_TRANSFORM (filter);
281
282 gst_base_transform_set_gap_aware (base, TRUE);
283
284 filter->num_tracks = 0;
285 filter->forced = FORCED_DEFAULT;
286 filter->message = DEFAULT_MESSAGE;
287 filter->reference_level = RG_REFERENCE_LEVEL;
288
289 filter->ctx = NULL;
290 filter->analyze = NULL;
291 }
292
293 static void
gst_rg_analysis_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)294 gst_rg_analysis_set_property (GObject * object, guint prop_id,
295 const GValue * value, GParamSpec * pspec)
296 {
297 GstRgAnalysis *filter = GST_RG_ANALYSIS (object);
298
299 GST_OBJECT_LOCK (filter);
300 switch (prop_id) {
301 case PROP_NUM_TRACKS:
302 filter->num_tracks = g_value_get_int (value);
303 break;
304 case PROP_FORCED:
305 filter->forced = g_value_get_boolean (value);
306 break;
307 case PROP_REFERENCE_LEVEL:
308 filter->reference_level = g_value_get_double (value);
309 break;
310 case PROP_MESSAGE:
311 filter->message = g_value_get_boolean (value);
312 break;
313 default:
314 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 break;
316 }
317 GST_OBJECT_UNLOCK (filter);
318 }
319
320 static void
gst_rg_analysis_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)321 gst_rg_analysis_get_property (GObject * object, guint prop_id,
322 GValue * value, GParamSpec * pspec)
323 {
324 GstRgAnalysis *filter = GST_RG_ANALYSIS (object);
325
326 GST_OBJECT_LOCK (filter);
327 switch (prop_id) {
328 case PROP_NUM_TRACKS:
329 g_value_set_int (value, filter->num_tracks);
330 break;
331 case PROP_FORCED:
332 g_value_set_boolean (value, filter->forced);
333 break;
334 case PROP_REFERENCE_LEVEL:
335 g_value_set_double (value, filter->reference_level);
336 break;
337 case PROP_MESSAGE:
338 g_value_set_boolean (value, filter->message);
339 break;
340 default:
341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 break;
343 }
344 GST_OBJECT_UNLOCK (filter);
345 }
346
347 static void
gst_rg_analysis_post_message(gpointer rganalysis,GstClockTime timestamp,GstClockTime duration,gdouble rglevel)348 gst_rg_analysis_post_message (gpointer rganalysis, GstClockTime timestamp,
349 GstClockTime duration, gdouble rglevel)
350 {
351 GstRgAnalysis *filter = GST_RG_ANALYSIS (rganalysis);
352 if (filter->message) {
353 GstMessage *m;
354
355 m = gst_message_new_element (GST_OBJECT_CAST (rganalysis),
356 gst_structure_new ("rganalysis",
357 "timestamp", G_TYPE_UINT64, timestamp,
358 "duration", G_TYPE_UINT64, duration,
359 "rglevel", G_TYPE_DOUBLE, rglevel, NULL));
360
361 gst_element_post_message (GST_ELEMENT_CAST (rganalysis), m);
362 }
363 }
364
365
366 static gboolean
gst_rg_analysis_start(GstBaseTransform * base)367 gst_rg_analysis_start (GstBaseTransform * base)
368 {
369 GstRgAnalysis *filter = GST_RG_ANALYSIS (base);
370
371 filter->ignore_tags = FALSE;
372 filter->skip = FALSE;
373 filter->has_track_gain = FALSE;
374 filter->has_track_peak = FALSE;
375 filter->has_album_gain = FALSE;
376 filter->has_album_peak = FALSE;
377
378 filter->ctx = rg_analysis_new ();
379 GST_OBJECT_LOCK (filter);
380 rg_analysis_init_silence_detection (filter->ctx, gst_rg_analysis_post_message,
381 filter);
382 GST_OBJECT_UNLOCK (filter);
383 filter->analyze = NULL;
384
385 GST_LOG_OBJECT (filter, "started");
386
387 return TRUE;
388 }
389
390 static gboolean
gst_rg_analysis_set_caps(GstBaseTransform * base,GstCaps * in_caps,GstCaps * out_caps)391 gst_rg_analysis_set_caps (GstBaseTransform * base, GstCaps * in_caps,
392 GstCaps * out_caps)
393 {
394 GstRgAnalysis *filter = GST_RG_ANALYSIS (base);
395 GstAudioInfo info;
396 gint rate, channels;
397
398 g_return_val_if_fail (filter->ctx != NULL, FALSE);
399
400 GST_DEBUG_OBJECT (filter,
401 "set_caps in %" GST_PTR_FORMAT " out %" GST_PTR_FORMAT,
402 in_caps, out_caps);
403
404 if (!gst_audio_info_from_caps (&info, in_caps))
405 goto invalid_format;
406
407 rate = GST_AUDIO_INFO_RATE (&info);
408
409 if (!rg_analysis_set_sample_rate (filter->ctx, rate))
410 goto invalid_format;
411
412 channels = GST_AUDIO_INFO_CHANNELS (&info);
413
414 if (channels < 1 || channels > 2)
415 goto invalid_format;
416
417 switch (GST_AUDIO_INFO_FORMAT (&info)) {
418 case GST_AUDIO_FORMAT_F32:
419 /* The depth is not variable for float formats of course. It just
420 * makes the transform function nice and simple if the
421 * rg_analysis_analyze_* functions have a common signature. */
422 filter->depth = sizeof (gfloat) * 8;
423
424 if (channels == 1)
425 filter->analyze = rg_analysis_analyze_mono_float;
426 else
427 filter->analyze = rg_analysis_analyze_stereo_float;
428
429 break;
430 case GST_AUDIO_FORMAT_S16:
431 filter->depth = sizeof (gint16) * 8;
432
433 if (channels == 1)
434 filter->analyze = rg_analysis_analyze_mono_int16;
435 else
436 filter->analyze = rg_analysis_analyze_stereo_int16;
437 break;
438 default:
439 goto invalid_format;
440 }
441
442 return TRUE;
443
444 /* Errors. */
445 invalid_format:
446 {
447 filter->analyze = NULL;
448 GST_ELEMENT_ERROR (filter, CORE, NEGOTIATION,
449 ("Invalid incoming caps: %" GST_PTR_FORMAT, in_caps), (NULL));
450 return FALSE;
451 }
452 }
453
454 static GstFlowReturn
gst_rg_analysis_transform_ip(GstBaseTransform * base,GstBuffer * buf)455 gst_rg_analysis_transform_ip (GstBaseTransform * base, GstBuffer * buf)
456 {
457 GstRgAnalysis *filter = GST_RG_ANALYSIS (base);
458 GstMapInfo map;
459
460 g_return_val_if_fail (filter->ctx != NULL, GST_FLOW_FLUSHING);
461 g_return_val_if_fail (filter->analyze != NULL, GST_FLOW_NOT_NEGOTIATED);
462
463 if (filter->skip)
464 return GST_FLOW_OK;
465
466 gst_buffer_map (buf, &map, GST_MAP_READ);
467 GST_LOG_OBJECT (filter, "processing buffer of size %" G_GSIZE_FORMAT,
468 map.size);
469
470 rg_analysis_start_buffer (filter->ctx, GST_BUFFER_TIMESTAMP (buf));
471 filter->analyze (filter->ctx, map.data, map.size, filter->depth);
472
473 gst_buffer_unmap (buf, &map);
474
475 return GST_FLOW_OK;
476 }
477
478 static gboolean
gst_rg_analysis_sink_event(GstBaseTransform * base,GstEvent * event)479 gst_rg_analysis_sink_event (GstBaseTransform * base, GstEvent * event)
480 {
481 GstRgAnalysis *filter = GST_RG_ANALYSIS (base);
482
483 g_return_val_if_fail (filter->ctx != NULL, TRUE);
484
485 switch (GST_EVENT_TYPE (event)) {
486
487 case GST_EVENT_EOS:
488 {
489 GST_LOG_OBJECT (filter, "received EOS event");
490
491 gst_rg_analysis_handle_eos (filter);
492
493 GST_LOG_OBJECT (filter, "passing on EOS event");
494
495 break;
496 }
497 case GST_EVENT_TAG:
498 {
499 GstTagList *tag_list;
500
501 /* The reference to the tag list is borrowed. */
502 gst_event_parse_tag (event, &tag_list);
503 gst_rg_analysis_handle_tags (filter, tag_list);
504
505 break;
506 }
507 default:
508 break;
509 }
510
511 return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (base, event);
512 }
513
514 static gboolean
gst_rg_analysis_stop(GstBaseTransform * base)515 gst_rg_analysis_stop (GstBaseTransform * base)
516 {
517 GstRgAnalysis *filter = GST_RG_ANALYSIS (base);
518
519 g_return_val_if_fail (filter->ctx != NULL, FALSE);
520
521 rg_analysis_destroy (filter->ctx);
522 filter->ctx = NULL;
523
524 GST_LOG_OBJECT (filter, "stopped");
525
526 return TRUE;
527 }
528
529 /* FIXME: handle global vs. stream-tags? */
530 static void
gst_rg_analysis_handle_tags(GstRgAnalysis * filter,const GstTagList * tag_list)531 gst_rg_analysis_handle_tags (GstRgAnalysis * filter,
532 const GstTagList * tag_list)
533 {
534 gboolean album_processing = (filter->num_tracks > 0);
535 gdouble dummy;
536
537 if (!album_processing)
538 filter->ignore_tags = FALSE;
539
540 if (filter->skip && album_processing) {
541 GST_DEBUG_OBJECT (filter, "ignoring tag event: skipping album");
542 return;
543 } else if (filter->skip) {
544 GST_DEBUG_OBJECT (filter, "ignoring tag event: skipping track");
545 return;
546 } else if (filter->ignore_tags) {
547 GST_DEBUG_OBJECT (filter, "ignoring tag event: cannot skip anyways");
548 return;
549 }
550
551 filter->has_track_gain |= gst_tag_list_get_double (tag_list,
552 GST_TAG_TRACK_GAIN, &dummy);
553 filter->has_track_peak |= gst_tag_list_get_double (tag_list,
554 GST_TAG_TRACK_PEAK, &dummy);
555 filter->has_album_gain |= gst_tag_list_get_double (tag_list,
556 GST_TAG_ALBUM_GAIN, &dummy);
557 filter->has_album_peak |= gst_tag_list_get_double (tag_list,
558 GST_TAG_ALBUM_PEAK, &dummy);
559
560 if (!(filter->has_track_gain && filter->has_track_peak)) {
561 GST_DEBUG_OBJECT (filter, "track tags not complete yet");
562 return;
563 }
564
565 if (album_processing && !(filter->has_album_gain && filter->has_album_peak)) {
566 GST_DEBUG_OBJECT (filter, "album tags not complete yet");
567 return;
568 }
569
570 if (filter->forced) {
571 GST_DEBUG_OBJECT (filter,
572 "existing tags are sufficient, but processing anyway (forced)");
573 return;
574 }
575
576 filter->skip = TRUE;
577 rg_analysis_reset (filter->ctx);
578
579 if (!album_processing) {
580 GST_DEBUG_OBJECT (filter,
581 "existing tags are sufficient, will not process this track");
582 } else {
583 GST_DEBUG_OBJECT (filter,
584 "existing tags are sufficient, will not process this album");
585 }
586 }
587
588 static void
gst_rg_analysis_handle_eos(GstRgAnalysis * filter)589 gst_rg_analysis_handle_eos (GstRgAnalysis * filter)
590 {
591 gboolean album_processing = (filter->num_tracks > 0);
592 gboolean album_finished = (filter->num_tracks == 1);
593 gboolean album_skipping = album_processing && filter->skip;
594
595 filter->has_track_gain = FALSE;
596 filter->has_track_peak = FALSE;
597
598 if (album_finished) {
599 filter->ignore_tags = FALSE;
600 filter->skip = FALSE;
601 filter->has_album_gain = FALSE;
602 filter->has_album_peak = FALSE;
603 } else if (!album_skipping) {
604 filter->skip = FALSE;
605 }
606
607 /* We might have just fully processed a track because it has
608 * incomplete tags. If we do album processing and allow skipping
609 * (not forced), prevent switching to skipping if a later track with
610 * full tags comes along: */
611 if (!filter->forced && album_processing && !album_finished)
612 filter->ignore_tags = TRUE;
613
614 if (!filter->skip) {
615 GstTagList *tag_list = NULL;
616 gboolean track_success;
617 gboolean album_success = FALSE;
618
619 track_success = gst_rg_analysis_track_result (filter, &tag_list);
620
621 if (album_finished)
622 album_success = gst_rg_analysis_album_result (filter, &tag_list);
623 else if (!album_processing)
624 rg_analysis_reset_album (filter->ctx);
625
626 if (track_success || album_success) {
627 GST_LOG_OBJECT (filter, "posting tag list with results");
628 gst_tag_list_add (tag_list, GST_TAG_MERGE_APPEND,
629 GST_TAG_REFERENCE_LEVEL, filter->reference_level, NULL);
630 /* This takes ownership of our reference to the list */
631 gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (filter),
632 gst_event_new_tag (tag_list));
633 tag_list = NULL;
634 }
635 }
636
637 if (album_processing) {
638 filter->num_tracks--;
639
640 if (!album_finished) {
641 GST_DEBUG_OBJECT (filter, "album not finished yet (num-tracks is now %u)",
642 filter->num_tracks);
643 } else {
644 GST_DEBUG_OBJECT (filter, "album finished (num-tracks is now 0)");
645 }
646 }
647
648 if (album_processing)
649 g_object_notify (G_OBJECT (filter), "num-tracks");
650 }
651
652 /* FIXME: return tag list (lists?) based on input tags.. */
653 static gboolean
gst_rg_analysis_track_result(GstRgAnalysis * filter,GstTagList ** tag_list)654 gst_rg_analysis_track_result (GstRgAnalysis * filter, GstTagList ** tag_list)
655 {
656 gboolean track_success;
657 gdouble track_gain, track_peak;
658
659 track_success = rg_analysis_track_result (filter->ctx, &track_gain,
660 &track_peak);
661
662 if (track_success) {
663 track_gain += filter->reference_level - RG_REFERENCE_LEVEL;
664 GST_INFO_OBJECT (filter, "track gain is %+.2f dB, peak %.6f", track_gain,
665 track_peak);
666 } else {
667 GST_INFO_OBJECT (filter, "track was too short to analyze");
668 }
669
670 if (track_success) {
671 if (*tag_list == NULL)
672 *tag_list = gst_tag_list_new_empty ();
673 gst_tag_list_add (*tag_list, GST_TAG_MERGE_APPEND,
674 GST_TAG_TRACK_PEAK, track_peak, GST_TAG_TRACK_GAIN, track_gain, NULL);
675 }
676
677 return track_success;
678 }
679
680 static gboolean
gst_rg_analysis_album_result(GstRgAnalysis * filter,GstTagList ** tag_list)681 gst_rg_analysis_album_result (GstRgAnalysis * filter, GstTagList ** tag_list)
682 {
683 gboolean album_success;
684 gdouble album_gain, album_peak;
685
686 album_success = rg_analysis_album_result (filter->ctx, &album_gain,
687 &album_peak);
688
689 if (album_success) {
690 album_gain += filter->reference_level - RG_REFERENCE_LEVEL;
691 GST_INFO_OBJECT (filter, "album gain is %+.2f dB, peak %.6f", album_gain,
692 album_peak);
693 } else {
694 GST_INFO_OBJECT (filter, "album was too short to analyze");
695 }
696
697 if (album_success) {
698 if (*tag_list == NULL)
699 *tag_list = gst_tag_list_new_empty ();
700 gst_tag_list_add (*tag_list, GST_TAG_MERGE_APPEND,
701 GST_TAG_ALBUM_PEAK, album_peak, GST_TAG_ALBUM_GAIN, album_gain, NULL);
702 }
703
704 return album_success;
705 }
706