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"
33 #include <libavutil/channel_layout.h>
34 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
35 #include <libavfilter/f_ebur128.h>
38 #include <boost/foreach.hpp>
48 using boost::shared_ptr;
49 using boost::dynamic_pointer_cast;
50 using namespace dcpomatic;
52 int const AnalyseAudioJob::_num_points = 1024;
54 /** @param from_zero true to analyse audio from time 0 in the playlist, otherwise begin at Playlist::start */
55 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool from_zero)
57 , _playlist (playlist)
58 , _path (film->audio_analysis_path(playlist))
59 , _from_zero (from_zero)
61 , _samples_per_point (1)
63 , _sample_peak (new float[film->audio_channels()])
64 , _sample_peak_frame (new Frame[film->audio_channels()])
65 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
66 , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), film->audio_channels()))
69 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
70 _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
71 _ebur128->setup (_filters);
74 for (int i = 0; i < film->audio_channels(); ++i) {
76 _sample_peak_frame[i] = 0;
80 _start = _playlist->start().get_value_or(DCPTime());
84 AnalyseAudioJob::~AnalyseAudioJob ()
87 BOOST_FOREACH (Filter const * i, _filters) {
88 delete const_cast<Filter*> (i);
91 delete[] _sample_peak;
92 delete[] _sample_peak_frame;
96 AnalyseAudioJob::name () const
98 return _("Analysing audio");
102 AnalyseAudioJob::json_name () const
104 return N_("analyse_audio");
108 AnalyseAudioJob::run ()
110 shared_ptr<Player> player (new Player(_film, _playlist, _playlist->length(_film)));
111 player->set_ignore_video ();
112 player->set_ignore_text ();
114 player->set_play_referenced ();
115 player->Audio.connect (bind (&AnalyseAudioJob::analyse, this, _1, _2));
117 DCPTime const length = _playlist->length (_film);
119 Frame const len = DCPTime (length - _start).frames_round (_film->audio_frame_rate());
120 _samples_per_point = max (int64_t (1), len / _num_points);
123 _current = new AudioPoint[_film->audio_channels ()];
124 _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
126 bool has_any_audio = false;
127 BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
129 has_any_audio = true;
134 player->seek (_start, true);
136 while (!player->pass ()) {}
139 vector<AudioAnalysis::PeakTime> sample_peak;
140 for (int i = 0; i < _film->audio_channels(); ++i) {
141 sample_peak.push_back (
142 AudioAnalysis::PeakTime (_sample_peak[i], DCPTime::from_frames (_sample_peak_frame[i], _film->audio_frame_rate ()))
145 _analysis->set_sample_peak (sample_peak);
147 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
148 if (Config::instance()->analyse_ebur128 ()) {
149 void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
150 vector<float> true_peak;
151 for (int i = 0; i < _film->audio_channels(); ++i) {
152 true_peak.push_back (av_ebur128_get_true_peaks(eb)[i]);
154 _analysis->set_true_peak (true_peak);
155 _analysis->set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
156 _analysis->set_loudness_range (av_ebur128_get_loudness_range(eb));
160 if (_playlist->content().size() == 1) {
161 /* If there was only one piece of content in this analysis we may later need to know what its
162 gain was when we analysed it.
164 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
166 _analysis->set_analysis_gain (ac->gain());
170 _analysis->set_samples_per_point (_samples_per_point);
171 _analysis->set_sample_rate (_film->audio_frame_rate ());
172 _analysis->write (_path);
175 set_state (FINISHED_OK);
179 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
181 DCPOMATIC_ASSERT (time >= _start);
183 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
184 if (Config::instance()->analyse_ebur128 ()) {
185 _ebur128->process (b);
189 int const frames = b->frames ();
190 int const channels = b->channels ();
192 for (int j = 0; j < channels; ++j) {
193 float* data = b->data(j);
194 for (int i = 0; i < frames; ++i) {
196 float as = fabsf (s);
198 /* We may struggle to serialise and recover inf or -inf, so prevent such
199 values by replacing with this (140dB down) */
202 _current[j][AudioPoint::RMS] += pow (s, 2);
203 _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
205 if (as > _sample_peak[j]) {
206 _sample_peak[j] = as;
207 _sample_peak_frame[j] = _done + i;
210 if (((_done + i) % _samples_per_point) == 0) {
211 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
212 _analysis->add_point (j, _current[j]);
213 _current[j] = AudioPoint ();
220 DCPTime const length = _playlist->length (_film);
221 set_progress ((time.seconds() - _start.seconds()) / (length.seconds() - _start.seconds()));