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