Fix truncated log timestamps in at least some cases.
[dcpomatic.git] / src / lib / analyse_audio_job.cc
index 525ac6c911d9940fb0eb09ece3c2a344cda3d7b0..769f3762bee98ee214c9972ddb0c6018c0cc8767 100644 (file)
 #include "film.h"
 #include "player.h"
 #include "playlist.h"
+#include "filter.h"
+#include "audio_filter_graph.h"
+extern "C" {
+#include <libavutil/channel_layout.h>
+#ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
+#include <libavfilter/f_ebur128.h>
+#endif
+}
 #include <boost/foreach.hpp>
 #include <iostream>
 
@@ -45,14 +53,23 @@ AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const
        , _done (0)
        , _samples_per_point (1)
        , _current (0)
-       , _overall_peak (0)
-       , _overall_peak_frame (0)
+       , _sample_peak (0)
+       , _sample_peak_frame (0)
+#ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
+       , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), av_get_default_channel_layout(film->audio_channels())))
+#endif
 {
-
+#ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
+       _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
+       _ebur128->setup (_filters);
+#endif
 }
 
 AnalyseAudioJob::~AnalyseAudioJob ()
 {
+       BOOST_FOREACH (Filter const * i, _filters) {
+               delete const_cast<Filter*> (i);
+       }
        delete[] _current;
 }
 
@@ -97,12 +114,27 @@ AnalyseAudioJob::run ()
                _done = 0;
                DCPTime const block = DCPTime::from_seconds (1.0 / 8);
                for (DCPTime t = start; t < length; t += block) {
-                       analyse (player->get_audio (t, block, false));
+                       shared_ptr<const AudioBuffers> audio = player->get_audio (t, block, false);
+#ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
+                       _ebur128->process (audio);
+#endif
+                       analyse (audio);
                        set_progress ((t.seconds() - start.seconds()) / (length.seconds() - start.seconds()));
                }
        }
 
-       _analysis->set_peak (_overall_peak, DCPTime::from_frames (_overall_peak_frame, _film->audio_frame_rate ()));
+       _analysis->set_sample_peak (_sample_peak, DCPTime::from_frames (_sample_peak_frame, _film->audio_frame_rate ()));
+
+#ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
+       void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
+       double true_peak = 0;
+       for (int i = 0; i < _film->audio_channels(); ++i) {
+               true_peak = max (true_peak, av_ebur128_get_true_peaks(eb)[i]);
+       }
+       _analysis->set_true_peak (true_peak);
+       _analysis->set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
+       _analysis->set_loudness_range (av_ebur128_get_loudness_range(eb));
+#endif
 
        if (_playlist->content().size() == 1) {
                /* If there was only one piece of content in this analysis we may later need to know what its
@@ -138,9 +170,9 @@ AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
                        _current[j][AudioPoint::RMS] += pow (s, 2);
                        _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
 
-                       if (as > _overall_peak) {
-                               _overall_peak = as;
-                               _overall_peak_frame = _done + i;
+                       if (as > _sample_peak) {
+                               _sample_peak = as;
+                               _sample_peak_frame = _done + i;
                        }
 
                        if (((_done + i) % _samples_per_point) == 0) {