Updated cs_CZ translation from Tomáš Begeni.
[dcpomatic.git] / src / lib / audio_analyser.cc
index 3868e05b7196e4724e243d17acdfe78b11a476d9..6e7b9fa86971152e88755fe049c9ba68c3a9f9f2 100644 (file)
 #include "film.h"
 #include "filter.h"
 #include "playlist.h"
-#include "types.h"
-#include "warnings.h"
+#include <dcp/warnings.h>
 extern "C" {
 #include <leqm_nrt.h>
-DCPOMATIC_DISABLE_WARNINGS
+LIBDCP_DISABLE_WARNINGS
 #include <libavutil/channel_layout.h>
 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
 #include <libavfilter/f_ebur128.h>
 #endif
-DCPOMATIC_ENABLE_WARNINGS
+LIBDCP_ENABLE_WARNINGS
 }
 
 
@@ -53,26 +52,26 @@ 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 (new float[film->audio_channels()])
-       , _sample_peak_frame (new Frame[film->audio_channels()])
+       , _sample_peak (film->audio_channels())
+       , _sample_peak_frame (film->audio_channels())
        , _analysis (film->audio_channels())
 {
 
 #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 = new AudioPoint[_film->audio_channels()];
+       _current = std::vector<AudioPoint>(_film->audio_channels());
 
-       if (!from_zero) {
+       if (!whole_film) {
                _start = _playlist->start().get_value_or(DCPTime());
        }
 
@@ -87,8 +86,22 @@ AudioAnalyser::AudioAnalyser (shared_ptr<const Film> film, shared_ptr<const Play
                }
        };
 
+       auto content = _playlist->content();
+       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? */
-       vector<double> channel_corrections(film->audio_channels(), 1);
+       vector<double> channel_corrections(_leqm_channels, 1);
        add_if_required (channel_corrections,  4,   -3); // Ls
        add_if_required (channel_corrections,  5,   -3); // Rs
        add_if_required (channel_corrections,  6, -144); // HI
@@ -103,7 +116,7 @@ AudioAnalyser::AudioAnalyser (shared_ptr<const Film> film, shared_ptr<const Play
        add_if_required (channel_corrections, 15, -144); // Unused
 
        _leqm.reset(new leqm_nrt::Calculator(
-               film->audio_channels(),
+               _leqm_channels,
                film->audio_frame_rate(),
                24,
                channel_corrections,
@@ -119,39 +132,32 @@ AudioAnalyser::AudioAnalyser (shared_ptr<const Film> film, shared_ptr<const Play
 }
 
 
-AudioAnalyser::~AudioAnalyser ()
-{
-       delete[] _current;
-       for (auto i: _filters) {
-               delete const_cast<Filter*> (i);
-       }
-       delete[] _sample_peak;
-       delete[] _sample_peak_frame;
-}
-
-
 void
-AudioAnalyser::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
+AudioAnalyser::analyse (shared_ptr<AudioBuffers> b, DCPTime time)
 {
-       LOG_DEBUG_AUDIO_ANALYSIS("Received %1 frames at %2", b->frames(), to_string(time));
+       LOG_DEBUG_AUDIO_ANALYSIS("AudioAnalyser received %1 frames at %2", b->frames(), to_string(time));
        DCPOMATIC_ASSERT (time >= _start);
+       /* In bug #2364 we had a lot of frames arriving here (~47s worth) which
+        * caused an OOM error on Windows.  Check for the number of frames being
+        * reasonable here to make sure we catch this if it happens again.
+        */
+       DCPOMATIC_ASSERT(b->frames() < 480000);
 
 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
        if (Config::instance()->analyse_ebur128 ()) {
-               _ebur128->process (b);
+               _ebur128.process(b);
        }
 #endif
 
        int const frames = b->frames ();
-       int const channels = b->channels ();
-       vector<double> interleaved(frames * channels);
+       vector<double> interleaved(frames * _leqm_channels);
 
-       for (int j = 0; j < channels; ++j) {
-               float* data = b->data(j);
+       for (int j = 0; j < _leqm_channels; ++j) {
+               float const* data = b->data(j);
                for (int i = 0; i < frames; ++i) {
                        float s = data[i];
 
-                       interleaved[i * channels + j] = s;
+                       interleaved[i * _leqm_channels + j] = s;
 
                        float as = fabsf (s);
                        if (as < 10e-7) {
@@ -198,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]);