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