8d0accf0f62804bd4dfc44604621470774b9d681
[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::max;
45 using std::min;
46 using std::cout;
47 using boost::shared_ptr;
48 using boost::dynamic_pointer_cast;
49
50 int const AnalyseAudioJob::_num_points = 1024;
51
52 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
53         : Job (film)
54         , _playlist (playlist)
55         , _done (0)
56         , _samples_per_point (1)
57         , _current (0)
58         , _sample_peak (0)
59         , _sample_peak_frame (0)
60 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
61         , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), film->audio_channels()))
62 #endif
63 {
64 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
65         _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
66         _ebur128->setup (_filters);
67 #endif
68 }
69
70 AnalyseAudioJob::~AnalyseAudioJob ()
71 {
72         BOOST_FOREACH (Filter const * i, _filters) {
73                 delete const_cast<Filter*> (i);
74         }
75         delete[] _current;
76 }
77
78 string
79 AnalyseAudioJob::name () const
80 {
81         return _("Analyse audio");
82 }
83
84 string
85 AnalyseAudioJob::json_name () const
86 {
87         return N_("analyse_audio");
88 }
89
90 void
91 AnalyseAudioJob::run ()
92 {
93         shared_ptr<Player> player (new Player (_film, _playlist));
94         player->set_ignore_video ();
95         player->set_fast ();
96         player->set_play_referenced ();
97
98         DCPTime const start = _playlist->start().get_value_or (DCPTime ());
99         DCPTime const length = _playlist->length ();
100
101         Frame const len = DCPTime (length - start).frames_round (_film->audio_frame_rate());
102         _samples_per_point = max (int64_t (1), len / _num_points);
103
104         delete[] _current;
105         _current = new AudioPoint[_film->audio_channels ()];
106         _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
107
108         bool has_any_audio = false;
109         BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
110                 if (c->audio) {
111                         has_any_audio = true;
112                 }
113         }
114
115         if (has_any_audio) {
116                 _done = 0;
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);
123                         }
124 #endif
125                         analyse (audio);
126                         set_progress ((t.seconds() - start.seconds()) / (length.seconds() - start.seconds()));
127                 }
128         }
129
130         _analysis->set_sample_peak (_sample_peak, DCPTime::from_frames (_sample_peak_frame, _film->audio_frame_rate ()));
131
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]);
138                 }
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));
142         }
143 #endif
144
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.
148                 */
149                 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
150                 DCPOMATIC_ASSERT (ac);
151                 _analysis->set_analysis_gain (ac->gain ());
152         }
153
154         _analysis->write (_film->audio_analysis_path (_playlist));
155
156         set_progress (1);
157         set_state (FINISHED_OK);
158 }
159
160 void
161 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
162 {
163         int const frames = b->frames ();
164         int const channels = b->channels ();
165
166         for (int j = 0; j < channels; ++j) {
167                 float* data = b->data(j);
168                 for (int i = 0; i < frames; ++i) {
169                         float s = data[i];
170                         float as = fabsf (s);
171                         if (as < 10e-7) {
172                                 /* SafeStringStream can't serialise and recover inf or -inf, so prevent such
173                                    values by replacing with this (140dB down) */
174                                 s = as = 10e-7;
175                         }
176                         _current[j][AudioPoint::RMS] += pow (s, 2);
177                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
178
179                         if (as > _sample_peak) {
180                                 _sample_peak = as;
181                                 _sample_peak_frame = _done + i;
182                         }
183
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 ();
188                         }
189                 }
190         }
191
192         _done += frames;
193 }