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"
30 #include "audio_filter_graph.h"
34 #include <libavutil/channel_layout.h>
35 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
36 #include <libavfilter/f_ebur128.h>
39 #include <boost/foreach.hpp>
49 using boost::shared_ptr;
50 using boost::dynamic_pointer_cast;
51 using namespace dcpomatic;
53 int const AnalyseAudioJob::_num_points = 1024;
55 static void add_if_required(vector<double>& v, size_t i, double db)
58 v[i] = pow(10, db / 20);
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)
65 , _playlist (playlist)
66 , _path (film->audio_analysis_path(playlist))
67 , _from_zero (from_zero)
69 , _samples_per_point (1)
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()))
77 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
78 _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
79 _ebur128->setup (_filters);
82 for (int i = 0; i < film->audio_channels(); ++i) {
84 _sample_peak_frame[i] = 0;
88 _start = _playlist->start().get_value_or(DCPTime());
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
106 _leqm.reset(new leqm_nrt::Calculator(
107 film->audio_channels(),
108 film->audio_frame_rate(),
111 850, // suggested by leqm_nrt CLI source
112 64, // suggested by leqm_nrt CLI source
113 boost::thread::hardware_concurrency()
117 AnalyseAudioJob::~AnalyseAudioJob ()
120 BOOST_FOREACH (Filter const * i, _filters) {
121 delete const_cast<Filter*> (i);
124 delete[] _sample_peak;
125 delete[] _sample_peak_frame;
129 AnalyseAudioJob::name () const
131 return _("Analysing audio");
135 AnalyseAudioJob::json_name () const
137 return N_("analyse_audio");
141 AnalyseAudioJob::run ()
143 shared_ptr<Player> player (new Player(_film, _playlist, _playlist->length(_film)));
144 player->set_ignore_video ();
145 player->set_ignore_text ();
147 player->set_play_referenced ();
148 player->Audio.connect (bind (&AnalyseAudioJob::analyse, this, _1, _2));
150 DCPTime const length = _playlist->length (_film);
152 Frame const len = DCPTime (length - _start).frames_round (_film->audio_frame_rate());
153 _samples_per_point = max (int64_t (1), len / _num_points);
156 _current = new AudioPoint[_film->audio_channels ()];
157 _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
159 bool has_any_audio = false;
160 BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
162 has_any_audio = true;
167 player->seek (_start, true);
169 while (!player->pass ()) {}
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 ()))
178 _analysis->set_sample_peak (sample_peak);
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]);
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));
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.
197 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
199 _analysis->set_analysis_gain (ac->gain());
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);
209 set_state (FINISHED_OK);
213 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
215 DCPOMATIC_ASSERT (time >= _start);
217 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
218 if (Config::instance()->analyse_ebur128 ()) {
219 _ebur128->process (b);
223 int const frames = b->frames ();
224 int const channels = b->channels ();
225 vector<double> interleaved(frames * channels);
227 for (int j = 0; j < channels; ++j) {
228 float* data = b->data(j);
229 for (int i = 0; i < frames; ++i) {
232 interleaved[i * channels + j] = s;
234 float as = fabsf (s);
236 /* We may struggle to serialise and recover inf or -inf, so prevent such
237 values by replacing with this (140dB down) */
240 _current[j][AudioPoint::RMS] += pow (s, 2);
241 _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
243 if (as > _sample_peak[j]) {
244 _sample_peak[j] = as;
245 _sample_peak_frame[j] = _done + i;
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 ();
256 _leqm->add(interleaved);
260 DCPTime const length = _playlist->length (_film);
261 set_progress ((time.seconds() - _start.seconds()) / (length.seconds() - _start.seconds()));