2 Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
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"
31 #include "audio_filter_graph.h"
35 #include <libavutil/channel_layout.h>
36 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
37 #include <libavfilter/f_ebur128.h>
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;
56 int const AnalyseAudioJob::_num_points = 1024;
58 static void add_if_required(vector<double>& v, size_t i, double db)
61 v[i] = pow(10, db / 20);
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)
68 , _playlist (playlist)
69 , _path (film->audio_analysis_path(playlist))
70 , _from_zero (from_zero)
72 , _samples_per_point (1)
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()))
80 LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::AnalyseAudioJob");
82 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
83 _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
84 _ebur128->setup (_filters);
87 for (int i = 0; i < film->audio_channels(); ++i) {
89 _sample_peak_frame[i] = 0;
93 _start = _playlist->start().get_value_or(DCPTime());
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
111 _leqm.reset(new leqm_nrt::Calculator(
112 film->audio_channels(),
113 film->audio_frame_rate(),
116 850, // suggested by leqm_nrt CLI source
117 64, // suggested by leqm_nrt CLI source
118 boost::thread::hardware_concurrency()
122 AnalyseAudioJob::~AnalyseAudioJob ()
125 for (auto i: _filters) {
126 delete const_cast<Filter*> (i);
129 delete[] _sample_peak;
130 delete[] _sample_peak_frame;
134 AnalyseAudioJob::name () const
136 return _("Analysing audio");
140 AnalyseAudioJob::json_name () const
142 return N_("analyse_audio");
146 AnalyseAudioJob::run ()
148 LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::run");
150 shared_ptr<Player> player (new Player(_film, _playlist));
151 player->set_ignore_video ();
152 player->set_ignore_text ();
154 player->set_play_referenced ();
155 player->Audio.connect (bind (&AnalyseAudioJob::analyse, this, _1, _2));
157 DCPTime const length = _playlist->length (_film);
159 Frame const len = DCPTime (length - _start).frames_round (_film->audio_frame_rate());
160 _samples_per_point = max (int64_t (1), len / _num_points);
163 _current = new AudioPoint[_film->audio_channels ()];
164 _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
166 bool has_any_audio = false;
167 for (auto c: _playlist->content()) {
169 has_any_audio = true;
174 LOG_DEBUG_AUDIO_ANALYSIS("Seeking to %1", to_string(_start));
175 player->seek (_start, true);
177 LOG_DEBUG_AUDIO_ANALYSIS("Starting loop for playlist of length %1", to_string(length));
178 while (!player->pass ()) {}
181 LOG_DEBUG_AUDIO_ANALYSIS_NC("Loop complete");
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 ()))
189 _analysis->set_sample_peak (sample_peak);
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]);
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));
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.
208 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
210 _analysis->set_analysis_gain (ac->gain());
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);
219 LOG_DEBUG_AUDIO_ANALYSIS_NC("Job finished");
221 set_state (FINISHED_OK);
225 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
227 LOG_DEBUG_AUDIO_ANALYSIS("Received %1 frames at %2", b->frames(), to_string(time));
228 DCPOMATIC_ASSERT (time >= _start);
230 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
231 if (Config::instance()->analyse_ebur128 ()) {
232 _ebur128->process (b);
236 int const frames = b->frames ();
237 int const channels = b->channels ();
238 vector<double> interleaved(frames * channels);
240 for (int j = 0; j < channels; ++j) {
241 float* data = b->data(j);
242 for (int i = 0; i < frames; ++i) {
245 interleaved[i * channels + j] = s;
247 float as = fabsf (s);
249 /* We may struggle to serialise and recover inf or -inf, so prevent such
250 values by replacing with this (140dB down) */
253 _current[j][AudioPoint::RMS] += pow (s, 2);
254 _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
256 if (as > _sample_peak[j]) {
257 _sample_peak[j] = as;
258 _sample_peak_frame[j] = _done + i;
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 ();
269 _leqm->add(interleaved);
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");