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>
47 using boost::shared_ptr;
48 using boost::dynamic_pointer_cast;
50 int const AnalyseAudioJob::_num_points = 1024;
52 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
54 , _playlist (playlist)
56 , _samples_per_point (1)
59 , _sample_peak_frame (0)
60 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
61 , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), film->audio_channels()))
64 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
65 _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
66 _ebur128->setup (_filters);
70 AnalyseAudioJob::~AnalyseAudioJob ()
72 BOOST_FOREACH (Filter const * i, _filters) {
73 delete const_cast<Filter*> (i);
79 AnalyseAudioJob::name () const
81 return _("Analyse audio");
85 AnalyseAudioJob::json_name () const
87 return N_("analyse_audio");
91 AnalyseAudioJob::run ()
93 shared_ptr<Player> player (new Player (_film, _playlist));
94 player->set_ignore_video ();
96 player->set_play_referenced ();
98 DCPTime const start = _playlist->start().get_value_or (DCPTime ());
99 DCPTime const length = _playlist->length ();
101 Frame const len = DCPTime (length - start).frames_round (_film->audio_frame_rate());
102 _samples_per_point = max (int64_t (1), len / _num_points);
105 _current = new AudioPoint[_film->audio_channels ()];
106 _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
108 bool has_any_audio = false;
109 BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
111 has_any_audio = true;
117 DCPTime const block = DCPTime::from_seconds (1.0 / 8);
118 for (DCPTime t = start; t < length; t += block) {
119 shared_ptr<const AudioBuffers> audio = player->get_audio (t, block, false);
120 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
121 if (Config::instance()->analyse_ebur128 ()) {
122 _ebur128->process (audio);
126 set_progress ((t.seconds() - start.seconds()) / (length.seconds() - start.seconds()));
130 _analysis->set_sample_peak (_sample_peak, DCPTime::from_frames (_sample_peak_frame, _film->audio_frame_rate ()));
132 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
133 if (Config::instance()->analyse_ebur128 ()) {
134 void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
135 double true_peak = 0;
136 for (int i = 0; i < _film->audio_channels(); ++i) {
137 true_peak = max (true_peak, av_ebur128_get_true_peaks(eb)[i]);
139 _analysis->set_true_peak (true_peak);
140 _analysis->set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
141 _analysis->set_loudness_range (av_ebur128_get_loudness_range(eb));
145 if (_playlist->content().size() == 1) {
146 /* If there was only one piece of content in this analysis we may later need to know what its
147 gain was when we analysed it.
149 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
150 DCPOMATIC_ASSERT (ac);
151 _analysis->set_analysis_gain (ac->gain ());
154 _analysis->write (_film->audio_analysis_path (_playlist));
157 set_state (FINISHED_OK);
161 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
163 int const frames = b->frames ();
164 int const channels = b->channels ();
166 for (int j = 0; j < channels; ++j) {
167 float* data = b->data(j);
168 for (int i = 0; i < frames; ++i) {
170 float as = fabsf (s);
172 /* SafeStringStream can't serialise and recover inf or -inf, so prevent such
173 values by replacing with this (140dB down) */
176 _current[j][AudioPoint::RMS] += pow (s, 2);
177 _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
179 if (as > _sample_peak) {
181 _sample_peak_frame = _done + i;
184 if (((_done + i) % _samples_per_point) == 0) {
185 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
186 _analysis->add_point (j, _current[j]);
187 _current[j] = AudioPoint ();