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