2 Copyright (C) 2012-2015 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;
51 int const AnalyseAudioJob::_num_points = 1024;
53 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
55 , _playlist (playlist)
57 , _samples_per_point (1)
59 , _sample_peak (new float[film->audio_channels()])
60 , _sample_peak_frame (new Frame[film->audio_channels()])
61 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
62 , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), film->audio_channels()))
65 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
66 _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
67 _ebur128->setup (_filters);
70 for (int i = 0; i < film->audio_channels(); ++i) {
72 _sample_peak_frame[i] = 0;
76 AnalyseAudioJob::~AnalyseAudioJob ()
78 BOOST_FOREACH (Filter const * i, _filters) {
79 delete const_cast<Filter*> (i);
82 delete[] _sample_peak;
83 delete[] _sample_peak_frame;
87 AnalyseAudioJob::name () const
89 return _("Analyse audio");
93 AnalyseAudioJob::json_name () const
95 return N_("analyse_audio");
99 AnalyseAudioJob::run ()
101 shared_ptr<Player> player (new Player (_film, _playlist));
102 player->set_ignore_video ();
103 player->set_ignore_subtitle ();
105 player->set_play_referenced ();
106 player->Audio.connect (bind (&AnalyseAudioJob::analyse, this, _1, _2));
108 DCPTime const start = _playlist->start().get_value_or (DCPTime ());
109 DCPTime const length = _playlist->length ();
111 Frame const len = DCPTime (length - start).frames_round (_film->audio_frame_rate());
112 _samples_per_point = max (int64_t (1), len / _num_points);
115 _current = new AudioPoint[_film->audio_channels ()];
116 _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
118 bool has_any_audio = false;
119 BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
121 has_any_audio = true;
127 while (!player->pass ()) {}
130 vector<AudioAnalysis::PeakTime> sample_peak;
131 for (int i = 0; i < _film->audio_channels(); ++i) {
132 sample_peak.push_back (
133 AudioAnalysis::PeakTime (_sample_peak[i], DCPTime::from_frames (_sample_peak_frame[i], _film->audio_frame_rate ()))
136 _analysis->set_sample_peak (sample_peak);
138 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
139 if (Config::instance()->analyse_ebur128 ()) {
140 void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
141 vector<float> true_peak;
142 for (int i = 0; i < _film->audio_channels(); ++i) {
143 true_peak.push_back (av_ebur128_get_true_peaks(eb)[i]);
145 _analysis->set_true_peak (true_peak);
146 _analysis->set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
147 _analysis->set_loudness_range (av_ebur128_get_loudness_range(eb));
151 if (_playlist->content().size() == 1) {
152 /* If there was only one piece of content in this analysis we may later need to know what its
153 gain was when we analysed it.
155 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
156 DCPOMATIC_ASSERT (ac);
157 _analysis->set_analysis_gain (ac->gain ());
160 _analysis->write (_film->audio_analysis_path (_playlist));
163 set_state (FINISHED_OK);
167 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
169 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
170 if (Config::instance()->analyse_ebur128 ()) {
171 _ebur128->process (b);
175 int const frames = b->frames ();
176 int const channels = b->channels ();
178 for (int j = 0; j < channels; ++j) {
179 float* data = b->data(j);
180 for (int i = 0; i < frames; ++i) {
182 float as = fabsf (s);
184 /* We may struggle to serialise and recover inf or -inf, so prevent such
185 values by replacing with this (140dB down) */
188 _current[j][AudioPoint::RMS] += pow (s, 2);
189 _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
191 if (as > _sample_peak[j]) {
192 _sample_peak[j] = as;
193 _sample_peak_frame[j] = _done + i;
196 if (((_done + i) % _samples_per_point) == 0) {
197 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
198 _analysis->add_point (j, _current[j]);
199 _current[j] = AudioPoint ();
206 DCPTime const start = _playlist->start().get_value_or (DCPTime ());
207 DCPTime const length = _playlist->length ();
208 set_progress ((time.seconds() - start.seconds()) / (length.seconds() - start.seconds()));