Supporters update.
[dcpomatic.git] / src / lib / analyse_audio_job.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
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 "film.h"
27 #include "player.h"
28 #include "playlist.h"
29 #include "filter.h"
30 #include "audio_filter_graph.h"
31 #include "config.h"
32 extern "C" {
33 #include <libavutil/channel_layout.h>
34 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
35 #include <libavfilter/f_ebur128.h>
36 #endif
37 }
38 #include <boost/foreach.hpp>
39 #include <iostream>
40
41 #include "i18n.h"
42
43 using std::string;
44 using std::vector;
45 using std::max;
46 using std::min;
47 using std::cout;
48 using boost::shared_ptr;
49 using boost::dynamic_pointer_cast;
50 #if BOOST_VERSION >= 106100
51 using namespace boost::placeholders;
52 #endif
53
54 int const AnalyseAudioJob::_num_points = 1024;
55
56 /** @param from_zero true to analyse audio from time 0 in the playlist, otherwise begin at Playlist::start */
57 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, bool from_zero)
58         : Job (film)
59         , _playlist (playlist)
60         , _path (film->audio_analysis_path(playlist))
61         , _from_zero (from_zero)
62         , _done (0)
63         , _samples_per_point (1)
64         , _current (0)
65         , _sample_peak (new float[film->audio_channels()])
66         , _sample_peak_frame (new Frame[film->audio_channels()])
67 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
68         , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), film->audio_channels()))
69 #endif
70 {
71 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
72         _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
73         _ebur128->setup (_filters);
74 #endif
75
76         for (int i = 0; i < film->audio_channels(); ++i) {
77                 _sample_peak[i] = 0;
78                 _sample_peak_frame[i] = 0;
79         }
80
81         if (!_from_zero) {
82                 _start = _playlist->start().get_value_or(DCPTime());
83         }
84 }
85
86 AnalyseAudioJob::~AnalyseAudioJob ()
87 {
88         stop_thread ();
89         BOOST_FOREACH (Filter const * i, _filters) {
90                 delete const_cast<Filter*> (i);
91         }
92         delete[] _current;
93         delete[] _sample_peak;
94         delete[] _sample_peak_frame;
95 }
96
97 string
98 AnalyseAudioJob::name () const
99 {
100         return _("Analysing audio");
101 }
102
103 string
104 AnalyseAudioJob::json_name () const
105 {
106         return N_("analyse_audio");
107 }
108
109 void
110 AnalyseAudioJob::run ()
111 {
112         shared_ptr<Player> player (new Player (_film, _playlist));
113         player->set_ignore_video ();
114         player->set_ignore_text ();
115         player->set_fast ();
116         player->set_play_referenced ();
117         player->Audio.connect (bind (&AnalyseAudioJob::analyse, this, _1, _2));
118
119         DCPTime const length = _playlist->length (_film);
120
121         Frame const len = DCPTime (length - _start).frames_round (_film->audio_frame_rate());
122         _samples_per_point = max (int64_t (1), len / _num_points);
123
124         delete[] _current;
125         _current = new AudioPoint[_film->audio_channels ()];
126         _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
127
128         bool has_any_audio = false;
129         BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
130                 if (c->audio) {
131                         has_any_audio = true;
132                 }
133         }
134
135         if (has_any_audio) {
136                 player->seek (_start, true);
137                 _done = 0;
138                 while (!player->pass ()) {}
139         }
140
141         vector<AudioAnalysis::PeakTime> sample_peak;
142         for (int i = 0; i < _film->audio_channels(); ++i) {
143                 sample_peak.push_back (
144                         AudioAnalysis::PeakTime (_sample_peak[i], DCPTime::from_frames (_sample_peak_frame[i], _film->audio_frame_rate ()))
145                         );
146         }
147         _analysis->set_sample_peak (sample_peak);
148
149 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
150         if (Config::instance()->analyse_ebur128 ()) {
151                 void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
152                 vector<float> true_peak;
153                 for (int i = 0; i < _film->audio_channels(); ++i) {
154                         true_peak.push_back (av_ebur128_get_true_peaks(eb)[i]);
155                 }
156                 _analysis->set_true_peak (true_peak);
157                 _analysis->set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
158                 _analysis->set_loudness_range (av_ebur128_get_loudness_range(eb));
159         }
160 #endif
161
162         if (_playlist->content().size() == 1) {
163                 /* If there was only one piece of content in this analysis we may later need to know what its
164                    gain was when we analysed it.
165                 */
166                 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
167                 if (ac) {
168                         _analysis->set_analysis_gain (ac->gain());
169                 }
170         }
171
172         _analysis->set_samples_per_point (_samples_per_point);
173         _analysis->set_sample_rate (_film->audio_frame_rate ());
174         _analysis->write (_path);
175
176         set_progress (1);
177         set_state (FINISHED_OK);
178 }
179
180 void
181 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b, DCPTime time)
182 {
183         DCPOMATIC_ASSERT (time >= _start);
184
185 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
186         if (Config::instance()->analyse_ebur128 ()) {
187                 _ebur128->process (b);
188         }
189 #endif
190
191         int const frames = b->frames ();
192         int const channels = b->channels ();
193
194         for (int j = 0; j < channels; ++j) {
195                 float* data = b->data(j);
196                 for (int i = 0; i < frames; ++i) {
197                         float s = data[i];
198                         float as = fabsf (s);
199                         if (as < 10e-7) {
200                                 /* We may struggle to serialise and recover inf or -inf, so prevent such
201                                    values by replacing with this (140dB down) */
202                                 s = as = 10e-7;
203                         }
204                         _current[j][AudioPoint::RMS] += pow (s, 2);
205                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
206
207                         if (as > _sample_peak[j]) {
208                                 _sample_peak[j] = as;
209                                 _sample_peak_frame[j] = _done + i;
210                         }
211
212                         if (((_done + i) % _samples_per_point) == 0) {
213                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
214                                 _analysis->add_point (j, _current[j]);
215                                 _current[j] = AudioPoint ();
216                         }
217                 }
218         }
219
220         _done += frames;
221
222         DCPTime const length = _playlist->length (_film);
223         set_progress ((time.seconds() - _start.seconds()) / (length.seconds() - _start.seconds()));
224 }