Cleanup: handle Filter objects by value rather than by reference.
[dcpomatic.git] / src / lib / audio_analyser.cc
1 /*
2     Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "audio_analyser.h"
23 #include "audio_analysis.h"
24 #include "audio_buffers.h"
25 #include "audio_content.h"
26 #include "audio_filter_graph.h"
27 #include "audio_point.h"
28 #include "config.h"
29 #include "dcpomatic_log.h"
30 #include "film.h"
31 #include "filter.h"
32 #include "playlist.h"
33 #include <dcp/warnings.h>
34 extern "C" {
35 #include <leqm_nrt.h>
36 LIBDCP_DISABLE_WARNINGS
37 #include <libavutil/channel_layout.h>
38 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
39 #include <libavfilter/f_ebur128.h>
40 #endif
41 LIBDCP_ENABLE_WARNINGS
42 }
43
44
45 using std::make_shared;
46 using std::max;
47 using std::shared_ptr;
48 using std::vector;
49 using namespace dcpomatic;
50
51
52 static auto constexpr num_points = 1024;
53
54
55 AudioAnalyser::AudioAnalyser (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool from_zero, std::function<void (float)> set_progress)
56         : _film (film)
57         , _playlist (playlist)
58         , _set_progress (set_progress)
59 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
60         , _ebur128(film->audio_frame_rate(), film->audio_channels())
61 #endif
62         , _sample_peak (film->audio_channels())
63         , _sample_peak_frame (film->audio_channels())
64         , _analysis (film->audio_channels())
65 {
66
67 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
68         _filters.push_back({"ebur128", "ebur128", "audio", "ebur128=peak=true"});
69         _ebur128.setup(_filters);
70 #endif
71
72         _current = std::vector<AudioPoint>(_film->audio_channels());
73
74         if (!from_zero) {
75                 _start = _playlist->start().get_value_or(DCPTime());
76         }
77
78         for (int i = 0; i < film->audio_channels(); ++i) {
79                 _sample_peak[i] = 0;
80                 _sample_peak_frame[i] = 0;
81         }
82
83         auto add_if_required = [](vector<double>& v, size_t i, double db) {
84                 if (v.size() > i) {
85                         v[i] = pow(10, db / 20);
86                 }
87         };
88
89         _leqm_channels = film->audio_channels();
90         auto content = _playlist->content();
91         if (content.size() == 1 && content[0]->audio) {
92                 _leqm_channels = content[0]->audio->mapping().mapped_output_channels().size();
93         }
94
95         /* XXX: is this right?  Especially for more than 5.1? */
96         vector<double> channel_corrections(_leqm_channels, 1);
97         add_if_required (channel_corrections,  4,   -3); // Ls
98         add_if_required (channel_corrections,  5,   -3); // Rs
99         add_if_required (channel_corrections,  6, -144); // HI
100         add_if_required (channel_corrections,  7, -144); // VI
101         add_if_required (channel_corrections,  8,   -3); // Lc
102         add_if_required (channel_corrections,  9,   -3); // Rc
103         add_if_required (channel_corrections, 10,   -3); // Lc
104         add_if_required (channel_corrections, 11,   -3); // Rc
105         add_if_required (channel_corrections, 12, -144); // DBox
106         add_if_required (channel_corrections, 13, -144); // Sync
107         add_if_required (channel_corrections, 14, -144); // Sign Language
108         add_if_required (channel_corrections, 15, -144); // Unused
109
110         _leqm.reset(new leqm_nrt::Calculator(
111                 _leqm_channels,
112                 film->audio_frame_rate(),
113                 24,
114                 channel_corrections,
115                 850, // suggested by leqm_nrt CLI source
116                 64,  // suggested by leqm_nrt CLI source
117                 boost::thread::hardware_concurrency()
118                 ));
119
120         DCPTime const length = _playlist->length (_film);
121
122         Frame const len = DCPTime (length - _start).frames_round (film->audio_frame_rate());
123         _samples_per_point = max (int64_t (1), len / num_points);
124 }
125
126
127 void
128 AudioAnalyser::analyse (shared_ptr<AudioBuffers> b, DCPTime time)
129 {
130         LOG_DEBUG_AUDIO_ANALYSIS("AudioAnalyser received %1 frames at %2", b->frames(), to_string(time));
131         DCPOMATIC_ASSERT (time >= _start);
132         /* In bug #2364 we had a lot of frames arriving here (~47s worth) which
133          * caused an OOM error on Windows.  Check for the number of frames being
134          * reasonable here to make sure we catch this if it happens again.
135          */
136         DCPOMATIC_ASSERT(b->frames() < 480000);
137
138 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
139         if (Config::instance()->analyse_ebur128 ()) {
140                 _ebur128.process(b);
141         }
142 #endif
143
144         int const frames = b->frames ();
145         vector<double> interleaved(frames * _leqm_channels);
146
147         for (int j = 0; j < _leqm_channels; ++j) {
148                 float const* data = b->data(j);
149                 for (int i = 0; i < frames; ++i) {
150                         float s = data[i];
151
152                         interleaved[i * _leqm_channels + j] = s;
153
154                         float as = fabsf (s);
155                         if (as < 10e-7) {
156                                 /* We may struggle to serialise and recover inf or -inf, so prevent such
157                                    values by replacing with this (140dB down) */
158                                 s = as = 10e-7;
159                         }
160                         _current[j][AudioPoint::RMS] += pow (s, 2);
161                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
162
163                         if (as > _sample_peak[j]) {
164                                 _sample_peak[j] = as;
165                                 _sample_peak_frame[j] = _done + i;
166                         }
167
168                         if (((_done + i) % _samples_per_point) == 0) {
169                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
170                                 _analysis.add_point (j, _current[j]);
171                                 _current[j] = AudioPoint ();
172                         }
173                 }
174         }
175
176         _leqm->add(interleaved);
177
178         _done += frames;
179
180         DCPTime const length = _playlist->length (_film);
181         _set_progress ((time.seconds() - _start.seconds()) / (length.seconds() - _start.seconds()));
182         LOG_DEBUG_AUDIO_ANALYSIS_NC("Frames processed");
183 }
184
185
186 void
187 AudioAnalyser::finish ()
188 {
189         vector<AudioAnalysis::PeakTime> sample_peak;
190         for (int i = 0; i < _film->audio_channels(); ++i) {
191                 sample_peak.push_back (
192                         AudioAnalysis::PeakTime (_sample_peak[i], DCPTime::from_frames (_sample_peak_frame[i], _film->audio_frame_rate ()))
193                         );
194         }
195         _analysis.set_sample_peak (sample_peak);
196
197 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
198         if (Config::instance()->analyse_ebur128 ()) {
199                 void* eb = _ebur128.get("Parsed_ebur128_0")->priv;
200                 vector<float> true_peak;
201                 for (int i = 0; i < _film->audio_channels(); ++i) {
202                         true_peak.push_back (av_ebur128_get_true_peaks(eb)[i]);
203                 }
204                 _analysis.set_true_peak (true_peak);
205                 _analysis.set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
206                 _analysis.set_loudness_range (av_ebur128_get_loudness_range(eb));
207         }
208 #endif
209
210         if (_playlist->content().size() == 1) {
211                 /* If there was only one piece of content in this analysis we may later need to know what its
212                    gain was when we analysed it.
213                 */
214                 if (auto ac = _playlist->content().front()->audio) {
215                         _analysis.set_analysis_gain (ac->gain());
216                 }
217         }
218
219         _analysis.set_samples_per_point (_samples_per_point);
220         _analysis.set_sample_rate (_film->audio_frame_rate ());
221         _analysis.set_leqm (_leqm->leq_m());
222 }