Build fixes for Boost >= 1.73
[dcpomatic.git] / src / lib / analyse_audio_job.cc
1 /*
2     Copyright (C) 2012-2018 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 #include "audio_analysis.h"
22 #include "audio_buffers.h"
23 #include "analyse_audio_job.h"
24 #include "audio_content.h"
25 #include "compose.hpp"
26 #include "dcpomatic_log.h"
27 #include "film.h"
28 #include "player.h"
29 #include "playlist.h"
30 #include "filter.h"
31 #include "audio_filter_graph.h"
32 #include "config.h"
33 extern "C" {
34 #include <leqm_nrt.h>
35 #include <libavutil/channel_layout.h>
36 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
37 #include <libavfilter/f_ebur128.h>
38 #endif
39 }
40 #include <boost/foreach.hpp>
41 #include <iostream>
42
43 #include "i18n.h"
44
45 using std::string;
46 using std::vector;
47 using std::max;
48 using std::min;
49 using std::cout;
50 using boost::shared_ptr;
51 using boost::dynamic_pointer_cast;
52 using namespace dcpomatic;
53 #if BOOST_VERSION >= 106100
54 using namespace boost::placeholders;
55 #endif
56
57 int const AnalyseAudioJob::_num_points = 1024;
58
59 static void add_if_required(vector<double>& v, size_t i, double db)
60 {
61         if (v.size() > i) {
62                 v[i] = pow(10, db / 20);
63         }
64 }
65
66 /** @param from_zero true to analyse audio from time 0 in the playlist, otherwise begin at Playlist::start */
67 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool from_zero)
68         : Job (film)
69         , _playlist (playlist)
70         , _path (film->audio_analysis_path(playlist))
71         , _from_zero (from_zero)
72         , _done (0)
73         , _samples_per_point (1)
74         , _current (0)
75         , _sample_peak (new float[film->audio_channels()])
76         , _sample_peak_frame (new Frame[film->audio_channels()])
77 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
78         , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), film->audio_channels()))
79 #endif
80 {
81         LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::AnalyseAudioJob");
82
83 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
84         _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
85         _ebur128->setup (_filters);
86 #endif
87
88         for (int i = 0; i < film->audio_channels(); ++i) {
89                 _sample_peak[i] = 0;
90                 _sample_peak_frame[i] = 0;
91         }
92
93         if (!_from_zero) {
94                 _start = _playlist->start().get_value_or(DCPTime());
95         }
96
97         /* XXX: is this right?  Especially for more than 5.1? */
98         vector<double> channel_corrections(film->audio_channels(), 1);
99         add_if_required (channel_corrections,  4,   -3); // Ls
100         add_if_required (channel_corrections,  5,   -3); // Rs
101         add_if_required (channel_corrections,  6, -144); // HI
102         add_if_required (channel_corrections,  7, -144); // VI
103         add_if_required (channel_corrections,  8,   -3); // Lc
104         add_if_required (channel_corrections,  9,   -3); // Rc
105         add_if_required (channel_corrections, 10,   -3); // Lc
106         add_if_required (channel_corrections, 11,   -3); // Rc
107         add_if_required (channel_corrections, 12, -144); // DBox
108         add_if_required (channel_corrections, 13, -144); // Sync
109         add_if_required (channel_corrections, 14, -144); // Sign Language
110         add_if_required (channel_corrections, 15, -144); // Unused
111
112         _leqm.reset(new leqm_nrt::Calculator(
113                 film->audio_channels(),
114                 film->audio_frame_rate(),
115                 24,
116                 channel_corrections,
117                 850, // suggested by leqm_nrt CLI source
118                 64,  // suggested by leqm_nrt CLI source
119                 boost::thread::hardware_concurrency()
120                 ));
121 }
122
123 AnalyseAudioJob::~AnalyseAudioJob ()
124 {
125         stop_thread ();
126         BOOST_FOREACH (Filter const * i, _filters) {
127                 delete const_cast<Filter*> (i);
128         }
129         delete[] _current;
130         delete[] _sample_peak;
131         delete[] _sample_peak_frame;
132 }
133
134 string
135 AnalyseAudioJob::name () const
136 {
137         return _("Analysing audio");
138 }
139
140 string
141 AnalyseAudioJob::json_name () const
142 {
143         return N_("analyse_audio");
144 }
145
146 void
147 AnalyseAudioJob::run ()
148 {
149         LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::run");
150
151         shared_ptr<Player> player (new Player(_film, _playlist));
152         player->set_ignore_video ();
153         player->set_ignore_text ();
154         player->set_fast ();
155         player->set_play_referenced ();
156         player->Audio.connect (bind (&AnalyseAudioJob::analyse, this, _1, _2));
157
158         DCPTime const length = _playlist->length (_film);
159
160         Frame const len = DCPTime (length - _start).frames_round (_film->audio_frame_rate());
161         _samples_per_point = max (int64_t (1), len / _num_points);
162
163         delete[] _current;
164         _current = new AudioPoint[_film->audio_channels ()];
165         _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
166
167         bool has_any_audio = false;
168         BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
169                 if (c->audio) {
170                         has_any_audio = true;
171                 }
172         }
173
174         if (has_any_audio) {
175                 LOG_DEBUG_AUDIO_ANALYSIS("Seeking to %1", to_string(_start));
176                 player->seek (_start, true);
177                 _done = 0;
178                 LOG_DEBUG_AUDIO_ANALYSIS("Starting loop for playlist of length %1", to_string(length));
179                 while (!player->pass ()) {}
180         }
181
182         LOG_DEBUG_AUDIO_ANALYSIS_NC("Loop complete");
183
184         vector<AudioAnalysis::PeakTime> sample_peak;
185         for (int i = 0; i < _film->audio_channels(); ++i) {
186                 sample_peak.push_back (
187                         AudioAnalysis::PeakTime (_sample_peak[i], DCPTime::from_frames (_sample_peak_frame[i], _film->audio_frame_rate ()))
188                         );
189         }
190         _analysis->set_sample_peak (sample_peak);
191
192 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
193         if (Config::instance()->analyse_ebur128 ()) {
194                 void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
195                 vector<float> true_peak;
196                 for (int i = 0; i < _film->audio_channels(); ++i) {
197                         true_peak.push_back (av_ebur128_get_true_peaks(eb)[i]);
198                 }
199                 _analysis->set_true_peak (true_peak);
200                 _analysis->set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
201                 _analysis->set_loudness_range (av_ebur128_get_loudness_range(eb));
202         }
203 #endif
204
205         if (_playlist->content().size() == 1) {
206                 /* If there was only one piece of content in this analysis we may later need to know what its
207                    gain was when we analysed it.
208                 */
209                 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
210                 if (ac) {
211                         _analysis->set_analysis_gain (ac->gain());
212                 }
213         }
214
215         _analysis->set_samples_per_point (_samples_per_point);
216         _analysis->set_sample_rate (_film->audio_frame_rate ());
217         _analysis->set_leqm (_leqm->leq_m());
218         _analysis->write (_path);
219
220         LOG_DEBUG_AUDIO_ANALYSIS_NC("Job finished");
221         set_progress (1);
222         set_state (FINISHED_OK);
223 }
224
225 void
226 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
227 {
228         LOG_DEBUG_AUDIO_ANALYSIS("Received %1 frames at %2", b->frames(), to_string(time));
229         DCPOMATIC_ASSERT (time >= _start);
230
231 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
232         if (Config::instance()->analyse_ebur128 ()) {
233                 _ebur128->process (b);
234         }
235 #endif
236
237         int const frames = b->frames ();
238         int const channels = b->channels ();
239         vector<double> interleaved(frames * channels);
240
241         for (int j = 0; j < channels; ++j) {
242                 float* data = b->data(j);
243                 for (int i = 0; i < frames; ++i) {
244                         float s = data[i];
245
246                         interleaved[i * channels + j] = s;
247
248                         float as = fabsf (s);
249                         if (as < 10e-7) {
250                                 /* We may struggle to serialise and recover inf or -inf, so prevent such
251                                    values by replacing with this (140dB down) */
252                                 s = as = 10e-7;
253                         }
254                         _current[j][AudioPoint::RMS] += pow (s, 2);
255                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
256
257                         if (as > _sample_peak[j]) {
258                                 _sample_peak[j] = as;
259                                 _sample_peak_frame[j] = _done + i;
260                         }
261
262                         if (((_done + i) % _samples_per_point) == 0) {
263                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
264                                 _analysis->add_point (j, _current[j]);
265                                 _current[j] = AudioPoint ();
266                         }
267                 }
268         }
269
270         _leqm->add(interleaved);
271
272         _done += frames;
273
274         DCPTime const length = _playlist->length (_film);
275         set_progress ((time.seconds() - _start.seconds()) / (length.seconds() - _start.seconds()));
276         LOG_DEBUG_AUDIO_ANALYSIS_NC("Frames processed");
277 }