fefb8bc16240cf0b80c9a5b4af35ffb7b91971cc
[dcpomatic.git] / src / lib / analyse_audio_job.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "audio_analysis.h"
21 #include "audio_buffers.h"
22 #include "analyse_audio_job.h"
23 #include "audio_content.h"
24 #include "compose.hpp"
25 #include "film.h"
26 #include "player.h"
27 #include "playlist.h"
28 #include "filter.h"
29 #include "audio_filter_graph.h"
30 #include "config.h"
31 extern "C" {
32 #include <libavutil/channel_layout.h>
33 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
34 #include <libavfilter/f_ebur128.h>
35 #endif
36 }
37 #include <boost/foreach.hpp>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 using std::string;
43 using std::max;
44 using std::min;
45 using std::cout;
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
48
49 int const AnalyseAudioJob::_num_points = 1024;
50
51 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
52         : Job (film)
53         , _playlist (playlist)
54         , _done (0)
55         , _samples_per_point (1)
56         , _current (0)
57         , _sample_peak (0)
58         , _sample_peak_frame (0)
59 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
60         , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), film->audio_channels()))
61 #endif
62 {
63 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
64         _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
65         _ebur128->setup (_filters);
66 #endif
67 }
68
69 AnalyseAudioJob::~AnalyseAudioJob ()
70 {
71         BOOST_FOREACH (Filter const * i, _filters) {
72                 delete const_cast<Filter*> (i);
73         }
74         delete[] _current;
75 }
76
77 string
78 AnalyseAudioJob::name () const
79 {
80         return _("Analyse audio");
81 }
82
83 string
84 AnalyseAudioJob::json_name () const
85 {
86         return N_("analyse_audio");
87 }
88
89 void
90 AnalyseAudioJob::run ()
91 {
92         shared_ptr<Player> player (new Player (_film, _playlist));
93         player->set_ignore_video ();
94         player->set_fast ();
95         player->set_play_referenced ();
96
97         DCPTime const start = _playlist->start().get_value_or (DCPTime ());
98         DCPTime const length = _playlist->length ();
99
100         Frame const len = DCPTime (length - start).frames_round (_film->audio_frame_rate());
101         _samples_per_point = max (int64_t (1), len / _num_points);
102
103         delete[] _current;
104         _current = new AudioPoint[_film->audio_channels ()];
105         _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
106
107         bool has_any_audio = false;
108         BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
109                 if (c->audio) {
110                         has_any_audio = true;
111                 }
112         }
113
114         if (has_any_audio) {
115                 _done = 0;
116                 DCPTime const block = DCPTime::from_seconds (1.0 / 8);
117                 for (DCPTime t = start; t < length; t += block) {
118                         shared_ptr<const AudioBuffers> audio = player->get_audio (t, block, false);
119 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
120                         if (Config::instance()->analyse_ebur128 ()) {
121                                 _ebur128->process (audio);
122                         }
123 #endif
124                         analyse (audio);
125                         set_progress ((t.seconds() - start.seconds()) / (length.seconds() - start.seconds()));
126                 }
127         }
128
129         _analysis->set_sample_peak (_sample_peak, DCPTime::from_frames (_sample_peak_frame, _film->audio_frame_rate ()));
130
131 #ifdef DCPOMATIC_HAVE_EBUR128_PATCHED_FFMPEG
132         if (Config::instance()->analyse_ebur128 ()) {
133                 void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
134                 double true_peak = 0;
135                 for (int i = 0; i < _film->audio_channels(); ++i) {
136                         true_peak = max (true_peak, av_ebur128_get_true_peaks(eb)[i]);
137                 }
138                 _analysis->set_true_peak (true_peak);
139                 _analysis->set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
140                 _analysis->set_loudness_range (av_ebur128_get_loudness_range(eb));
141         }
142 #endif
143
144         if (_playlist->content().size() == 1) {
145                 /* If there was only one piece of content in this analysis we may later need to know what its
146                    gain was when we analysed it.
147                 */
148                 shared_ptr<const AudioContent> ac = _playlist->content().front()->audio;
149                 DCPOMATIC_ASSERT (ac);
150                 _analysis->set_analysis_gain (ac->gain ());
151         }
152
153         _analysis->write (_film->audio_analysis_path (_playlist));
154
155         set_progress (1);
156         set_state (FINISHED_OK);
157 }
158
159 void
160 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
161 {
162         int const frames = b->frames ();
163         int const channels = b->channels ();
164
165         for (int j = 0; j < channels; ++j) {
166                 float* data = b->data(j);
167                 for (int i = 0; i < frames; ++i) {
168                         float s = data[i];
169                         float as = fabsf (s);
170                         if (as < 10e-7) {
171                                 /* SafeStringStream can't serialise and recover inf or -inf, so prevent such
172                                    values by replacing with this (140dB down) */
173                                 s = as = 10e-7;
174                         }
175                         _current[j][AudioPoint::RMS] += pow (s, 2);
176                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
177
178                         if (as > _sample_peak) {
179                                 _sample_peak = as;
180                                 _sample_peak_frame = _done + i;
181                         }
182
183                         if (((_done + i) % _samples_per_point) == 0) {
184                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
185                                 _analysis->add_point (j, _current[j]);
186                                 _current[j] = AudioPoint ();
187                         }
188                 }
189         }
190
191         _done += frames;
192 }