Missed update to private test repo version.
[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 "types.h"
34 #include <dcp/warnings.h>
35 extern "C" {
36 #include <leqm_nrt.h>
37 LIBDCP_DISABLE_WARNINGS
38 #include <libavutil/channel_layout.h>
39 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
40 #include <libavfilter/f_ebur128.h>
41 #endif
42 LIBDCP_ENABLE_WARNINGS
43 }
44
45
46 using std::make_shared;
47 using std::max;
48 using std::shared_ptr;
49 using std::vector;
50 using namespace dcpomatic;
51
52
53 static auto constexpr num_points = 1024;
54
55
56 AudioAnalyser::AudioAnalyser (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool from_zero, std::function<void (float)> set_progress)
57         : _film (film)
58         , _playlist (playlist)
59         , _set_progress (set_progress)
60 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
61         , _ebur128 (new AudioFilterGraph(film->audio_frame_rate(), film->audio_channels()))
62 #endif
63         , _sample_peak (film->audio_channels())
64         , _sample_peak_frame (film->audio_channels())
65         , _analysis (film->audio_channels())
66 {
67
68 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
69         _filters.push_back (new Filter("ebur128", "ebur128", "audio", "ebur128=peak=true"));
70         _ebur128->setup (_filters);
71 #endif
72
73         _current = std::vector<AudioPoint>(_film->audio_channels());
74
75         if (!from_zero) {
76                 _start = _playlist->start().get_value_or(DCPTime());
77         }
78
79         for (int i = 0; i < film->audio_channels(); ++i) {
80                 _sample_peak[i] = 0;
81                 _sample_peak_frame[i] = 0;
82         }
83
84         auto add_if_required = [](vector<double>& v, size_t i, double db) {
85                 if (v.size() > i) {
86                         v[i] = pow(10, db / 20);
87                 }
88         };
89
90         int leqm_channels = film->audio_channels();
91         auto content = _playlist->content();
92         if (content.size() == 1 && content[0]->audio) {
93                 leqm_channels = content[0]->audio->mapping().mapped_output_channels().size();
94         }
95
96         /* XXX: is this right?  Especially for more than 5.1? */
97         vector<double> channel_corrections(leqm_channels, 1);
98         add_if_required (channel_corrections,  4,   -3); // Ls
99         add_if_required (channel_corrections,  5,   -3); // Rs
100         add_if_required (channel_corrections,  6, -144); // HI
101         add_if_required (channel_corrections,  7, -144); // VI
102         add_if_required (channel_corrections,  8,   -3); // Lc
103         add_if_required (channel_corrections,  9,   -3); // Rc
104         add_if_required (channel_corrections, 10,   -3); // Lc
105         add_if_required (channel_corrections, 11,   -3); // Rc
106         add_if_required (channel_corrections, 12, -144); // DBox
107         add_if_required (channel_corrections, 13, -144); // Sync
108         add_if_required (channel_corrections, 14, -144); // Sign Language
109         add_if_required (channel_corrections, 15, -144); // Unused
110
111         _leqm.reset(new leqm_nrt::Calculator(
112                 leqm_channels,
113                 film->audio_frame_rate(),
114                 24,
115                 channel_corrections,
116                 850, // suggested by leqm_nrt CLI source
117                 64,  // suggested by leqm_nrt CLI source
118                 boost::thread::hardware_concurrency()
119                 ));
120
121         DCPTime const length = _playlist->length (_film);
122
123         Frame const len = DCPTime (length - _start).frames_round (film->audio_frame_rate());
124         _samples_per_point = max (int64_t (1), len / num_points);
125 }
126
127
128 AudioAnalyser::~AudioAnalyser ()
129 {
130         for (auto i: _filters) {
131                 delete const_cast<Filter*> (i);
132         }
133 }
134
135
136 void
137 AudioAnalyser::analyse (shared_ptr<AudioBuffers> b, DCPTime time)
138 {
139         LOG_DEBUG_AUDIO_ANALYSIS("Received %1 frames at %2", b->frames(), to_string(time));
140         DCPOMATIC_ASSERT (time >= _start);
141
142 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
143         if (Config::instance()->analyse_ebur128 ()) {
144                 _ebur128->process (b);
145         }
146 #endif
147
148         int const frames = b->frames ();
149         int const channels = b->channels ();
150         vector<double> interleaved(frames * channels);
151
152         for (int j = 0; j < channels; ++j) {
153                 float const* data = b->data(j);
154                 for (int i = 0; i < frames; ++i) {
155                         float s = data[i];
156
157                         interleaved[i * channels + j] = s;
158
159                         float as = fabsf (s);
160                         if (as < 10e-7) {
161                                 /* We may struggle to serialise and recover inf or -inf, so prevent such
162                                    values by replacing with this (140dB down) */
163                                 s = as = 10e-7;
164                         }
165                         _current[j][AudioPoint::RMS] += pow (s, 2);
166                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
167
168                         if (as > _sample_peak[j]) {
169                                 _sample_peak[j] = as;
170                                 _sample_peak_frame[j] = _done + i;
171                         }
172
173                         if (((_done + i) % _samples_per_point) == 0) {
174                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
175                                 _analysis.add_point (j, _current[j]);
176                                 _current[j] = AudioPoint ();
177                         }
178                 }
179         }
180
181         _leqm->add(interleaved);
182
183         _done += frames;
184
185         DCPTime const length = _playlist->length (_film);
186         _set_progress ((time.seconds() - _start.seconds()) / (length.seconds() - _start.seconds()));
187         LOG_DEBUG_AUDIO_ANALYSIS_NC("Frames processed");
188 }
189
190
191 void
192 AudioAnalyser::finish ()
193 {
194         vector<AudioAnalysis::PeakTime> sample_peak;
195         for (int i = 0; i < _film->audio_channels(); ++i) {
196                 sample_peak.push_back (
197                         AudioAnalysis::PeakTime (_sample_peak[i], DCPTime::from_frames (_sample_peak_frame[i], _film->audio_frame_rate ()))
198                         );
199         }
200         _analysis.set_sample_peak (sample_peak);
201
202 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
203         if (Config::instance()->analyse_ebur128 ()) {
204                 void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
205                 vector<float> true_peak;
206                 for (int i = 0; i < _film->audio_channels(); ++i) {
207                         true_peak.push_back (av_ebur128_get_true_peaks(eb)[i]);
208                 }
209                 _analysis.set_true_peak (true_peak);
210                 _analysis.set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
211                 _analysis.set_loudness_range (av_ebur128_get_loudness_range(eb));
212         }
213 #endif
214
215         if (_playlist->content().size() == 1) {
216                 /* If there was only one piece of content in this analysis we may later need to know what its
217                    gain was when we analysed it.
218                 */
219                 if (auto ac = _playlist->content().front()->audio) {
220                         _analysis.set_analysis_gain (ac->gain());
221                 }
222         }
223
224         _analysis.set_samples_per_point (_samples_per_point);
225         _analysis.set_sample_rate (_film->audio_frame_rate ());
226         _analysis.set_leqm (_leqm->leq_m());
227 }