Add channel details to high-audio-level hints (#822).
[dcpomatic.git] / src / lib / analyse_audio_job.cc
1 /*
2     Copyright (C) 2012-2015 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
51 int const AnalyseAudioJob::_num_points = 1024;
52
53 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
54         : Job (film)
55         , _playlist (playlist)
56         , _done (0)
57         , _samples_per_point (1)
58         , _current (0)
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()))
63 #endif
64 {
65 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
66         _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
67         _ebur128->setup (_filters);
68 #endif
69 }
70
71 AnalyseAudioJob::~AnalyseAudioJob ()
72 {
73         BOOST_FOREACH (Filter const * i, _filters) {
74                 delete const_cast<Filter*> (i);
75         }
76         delete[] _current;
77         delete[] _sample_peak;
78         delete[] _sample_peak_frame;
79 }
80
81 string
82 AnalyseAudioJob::name () const
83 {
84         return _("Analyse audio");
85 }
86
87 string
88 AnalyseAudioJob::json_name () const
89 {
90         return N_("analyse_audio");
91 }
92
93 void
94 AnalyseAudioJob::run ()
95 {
96         shared_ptr<Player> player (new Player (_film, _playlist));
97         player->set_ignore_video ();
98         player->set_fast ();
99         player->set_play_referenced ();
100
101         DCPTime const start = _playlist->start().get_value_or (DCPTime ());
102         DCPTime const length = _playlist->length ();
103
104         Frame const len = DCPTime (length - start).frames_round (_film->audio_frame_rate());
105         _samples_per_point = max (int64_t (1), len / _num_points);
106
107         delete[] _current;
108         _current = new AudioPoint[_film->audio_channels ()];
109         _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
110
111         bool has_any_audio = false;
112         BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
113                 if (c->audio) {
114                         has_any_audio = true;
115                 }
116         }
117
118         if (has_any_audio) {
119                 _done = 0;
120                 DCPTime const block = DCPTime::from_seconds (1.0 / 8);
121                 for (DCPTime t = start; t < length; t += block) {
122                         shared_ptr<const AudioBuffers> audio = player->get_audio (t, block, false);
123 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
124                         if (Config::instance()->analyse_ebur128 ()) {
125                                 _ebur128->process (audio);
126                         }
127 #endif
128                         analyse (audio);
129                         set_progress ((t.seconds() - start.seconds()) / (length.seconds() - start.seconds()));
130                 }
131         }
132
133         vector<AudioAnalysis::PeakTime> sample_peak;
134         for (int i = 0; i < _film->audio_channels(); ++i) {
135                 sample_peak.push_back (
136                         AudioAnalysis::PeakTime (_sample_peak[i], DCPTime::from_frames (_sample_peak_frame[i], _film->audio_frame_rate ()))
137                         );
138         }
139         _analysis->set_sample_peak (sample_peak);
140
141 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
142         if (Config::instance()->analyse_ebur128 ()) {
143                 void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
144                 vector<float> true_peak;
145                 for (int i = 0; i < _film->audio_channels(); ++i) {
146                         true_peak.push_back (av_ebur128_get_true_peaks(eb)[i]);
147                 }
148                 _analysis->set_true_peak (true_peak);
149                 _analysis->set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
150                 _analysis->set_loudness_range (av_ebur128_get_loudness_range(eb));
151         }
152 #endif
153
154         if (_playlist->content().size() == 1) {
155                 /* If there was only one piece of content in this analysis we may later need to know what its
156                    gain was when we analysed it.
157                 */
158                 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
159                 DCPOMATIC_ASSERT (ac);
160                 _analysis->set_analysis_gain (ac->gain ());
161         }
162
163         _analysis->write (_film->audio_analysis_path (_playlist));
164
165         set_progress (1);
166         set_state (FINISHED_OK);
167 }
168
169 void
170 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
171 {
172         int const frames = b->frames ();
173         int const channels = b->channels ();
174
175         for (int j = 0; j < channels; ++j) {
176                 float* data = b->data(j);
177                 for (int i = 0; i < frames; ++i) {
178                         float s = data[i];
179                         float as = fabsf (s);
180                         if (as < 10e-7) {
181                                 /* We may struggle to serialise and recover inf or -inf, so prevent such
182                                    values by replacing with this (140dB down) */
183                                 s = as = 10e-7;
184                         }
185                         _current[j][AudioPoint::RMS] += pow (s, 2);
186                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
187
188                         if (as > _sample_peak[j]) {
189                                 _sample_peak[j] = as;
190                                 _sample_peak_frame[j] = _done + i;
191                         }
192
193                         if (((_done + i) % _samples_per_point) == 0) {
194                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
195                                 _analysis->add_point (j, _current[j]);
196                                 _current[j] = AudioPoint ();
197                         }
198                 }
199         }
200
201         _done += frames;
202 }