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