Work around deadlock when destroying J2KEncoder with a full writer queue (#2784).
[dcpomatic.git] / src / lib / audio_analyser.cc
index 8cbc3145b40d8e5dbc4bf4f0ce9c64324aa56392..6e7b9fa86971152e88755fe049c9ba68c3a9f9f2 100644 (file)
@@ -52,12 +52,12 @@ using namespace dcpomatic;
 static auto constexpr num_points = 1024;
 
 
-AudioAnalyser::AudioAnalyser (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool from_zero, std::function<void (float)> set_progress)
+AudioAnalyser::AudioAnalyser(shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool whole_film, std::function<void (float)> set_progress)
        : _film (film)
        , _playlist (playlist)
        , _set_progress (set_progress)
 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
-       , _ebur128 (new AudioFilterGraph(film->audio_frame_rate(), film->audio_channels()))
+       , _ebur128(film->audio_frame_rate(), film->audio_channels())
 #endif
        , _sample_peak (film->audio_channels())
        , _sample_peak_frame (film->audio_channels())
@@ -65,13 +65,13 @@ AudioAnalyser::AudioAnalyser (shared_ptr<const Film> film, shared_ptr<const Play
 {
 
 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
-       _filters.push_back (new Filter("ebur128", "ebur128", "audio", "ebur128=peak=true"));
-       _ebur128->setup (_filters);
+       _filters.push_back({"ebur128", "ebur128", "audio", "ebur128=peak=true"});
+       _ebur128.setup(_filters);
 #endif
 
        _current = std::vector<AudioPoint>(_film->audio_channels());
 
-       if (!from_zero) {
+       if (!whole_film) {
                _start = _playlist->start().get_value_or(DCPTime());
        }
 
@@ -86,10 +86,18 @@ AudioAnalyser::AudioAnalyser (shared_ptr<const Film> film, shared_ptr<const Play
                }
        };
 
-       _leqm_channels = film->audio_channels();
        auto content = _playlist->content();
-       if (content.size() == 1 && content[0]->audio) {
-               _leqm_channels = content[0]->audio->mapping().mapped_output_channels().size();
+       if (whole_film) {
+               _leqm_channels = film->audio_channels();
+       } else {
+               _leqm_channels = 0;
+               for (auto channel: content[0]->audio->mapping().mapped_output_channels()) {
+                       /* This means that if, for example, a file only maps C we will
+                        * calculate LEQ(m) for L, R and C.  I'm not sure if this is
+                        * right or not.
+                        */
+                       _leqm_channels = std::min(film->audio_channels(), channel + 1);
+               }
        }
 
        /* XXX: is this right?  Especially for more than 5.1? */
@@ -124,14 +132,6 @@ AudioAnalyser::AudioAnalyser (shared_ptr<const Film> film, shared_ptr<const Play
 }
 
 
-AudioAnalyser::~AudioAnalyser ()
-{
-       for (auto i: _filters) {
-               delete const_cast<Filter*> (i);
-       }
-}
-
-
 void
 AudioAnalyser::analyse (shared_ptr<AudioBuffers> b, DCPTime time)
 {
@@ -145,7 +145,7 @@ AudioAnalyser::analyse (shared_ptr<AudioBuffers> b, DCPTime time)
 
 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
        if (Config::instance()->analyse_ebur128 ()) {
-               _ebur128->process (b);
+               _ebur128.process(b);
        }
 #endif
 
@@ -204,7 +204,7 @@ AudioAnalyser::finish ()
 
 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
        if (Config::instance()->analyse_ebur128 ()) {
-               void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
+               void* eb = _ebur128.get("Parsed_ebur128_0")->priv;
                vector<float> true_peak;
                for (int i = 0; i < _film->audio_channels(); ++i) {
                        true_peak.push_back (av_ebur128_get_true_peaks(eb)[i]);