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>
40 #include <boost/foreach.hpp>
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;
57 int const AnalyseAudioJob::_num_points = 1024;
59 static void add_if_required(vector<double>& v, size_t i, double db)
62 v[i] = pow(10, db / 20);
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)
69 , _playlist (playlist)
70 , _path (film->audio_analysis_path(playlist))
71 , _from_zero (from_zero)
73 , _samples_per_point (1)
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()))
81 LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::AnalyseAudioJob");
83 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
84 _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
85 _ebur128->setup (_filters);
88 for (int i = 0; i < film->audio_channels(); ++i) {
90 _sample_peak_frame[i] = 0;
94 _start = _playlist->start().get_value_or(DCPTime());
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
112 _leqm.reset(new leqm_nrt::Calculator(
113 film->audio_channels(),
114 film->audio_frame_rate(),
117 850, // suggested by leqm_nrt CLI source
118 64, // suggested by leqm_nrt CLI source
119 boost::thread::hardware_concurrency()
123 AnalyseAudioJob::~AnalyseAudioJob ()
126 BOOST_FOREACH (Filter const * i, _filters) {
127 delete const_cast<Filter*> (i);
130 delete[] _sample_peak;
131 delete[] _sample_peak_frame;
135 AnalyseAudioJob::name () const
137 return _("Analysing audio");
141 AnalyseAudioJob::json_name () const
143 return N_("analyse_audio");
147 AnalyseAudioJob::run ()
149 LOG_DEBUG_AUDIO_ANALYSIS_NC("AnalyseAudioJob::run");
151 shared_ptr<Player> player (new Player(_film, _playlist));
152 player->set_ignore_video ();
153 player->set_ignore_text ();
155 player->set_play_referenced ();
156 player->Audio.connect (bind (&AnalyseAudioJob::analyse, this, _1, _2));
158 DCPTime const length = _playlist->length (_film);
160 Frame const len = DCPTime (length - _start).frames_round (_film->audio_frame_rate());
161 _samples_per_point = max (int64_t (1), len / _num_points);
164 _current = new AudioPoint[_film->audio_channels ()];
165 _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
167 bool has_any_audio = false;
168 BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
170 has_any_audio = true;
175 LOG_DEBUG_AUDIO_ANALYSIS("Seeking to %1", to_string(_start));
176 player->seek (_start, true);
178 LOG_DEBUG_AUDIO_ANALYSIS("Starting loop for playlist of length %1", to_string(length));
179 while (!player->pass ()) {}
182 LOG_DEBUG_AUDIO_ANALYSIS_NC("Loop complete");
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 ()))
190 _analysis->set_sample_peak (sample_peak);
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]);
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));
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.
209 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
211 _analysis->set_analysis_gain (ac->gain());
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);
220 LOG_DEBUG_AUDIO_ANALYSIS_NC("Job finished");
222 set_state (FINISHED_OK);
226 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
228 LOG_DEBUG_AUDIO_ANALYSIS("Received %1 frames at %2", b->frames(), to_string(time));
229 DCPOMATIC_ASSERT (time >= _start);
231 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
232 if (Config::instance()->analyse_ebur128 ()) {
233 _ebur128->process (b);
237 int const frames = b->frames ();
238 int const channels = b->channels ();
239 vector<double> interleaved(frames * channels);
241 for (int j = 0; j < channels; ++j) {
242 float* data = b->data(j);
243 for (int i = 0; i < frames; ++i) {
246 interleaved[i * channels + j] = s;
248 float as = fabsf (s);
250 /* We may struggle to serialise and recover inf or -inf, so prevent such
251 values by replacing with this (140dB down) */
254 _current[j][AudioPoint::RMS] += pow (s, 2);
255 _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
257 if (as > _sample_peak[j]) {
258 _sample_peak[j] = as;
259 _sample_peak_frame[j] = _done + i;
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 ();
270 _leqm->add(interleaved);
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");