Check for patched version of FFmpeg before using extra functions (#773).
[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 extern "C" {
31 #include <libavutil/channel_layout.h>
32 #ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
33 #include <libavfilter/f_ebur128.h>
34 #endif
35 }
36 #include <boost/foreach.hpp>
37 #include <iostream>
38
39 #include "i18n.h"
40
41 using std::string;
42 using std::max;
43 using std::min;
44 using std::cout;
45 using boost::shared_ptr;
46 using boost::dynamic_pointer_cast;
47
48 int const AnalyseAudioJob::_num_points = 1024;
49
50 AnalyseAudioJob::AnalyseAudioJob (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist)
51         : Job (film)
52         , _playlist (playlist)
53         , _done (0)
54         , _samples_per_point (1)
55         , _current (0)
56         , _sample_peak (0)
57         , _sample_peak_frame (0)
58 #ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
59         , _ebur128 (new AudioFilterGraph (film->audio_frame_rate(), av_get_default_channel_layout(film->audio_channels())))
60 #endif
61 {
62 #ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
63         _filters.push_back (new Filter ("ebur128", "ebur128", "audio", "ebur128=peak=true"));
64         _ebur128->setup (_filters);
65 #endif
66 }
67
68 AnalyseAudioJob::~AnalyseAudioJob ()
69 {
70         BOOST_FOREACH (Filter const * i, _filters) {
71                 delete const_cast<Filter*> (i);
72         }
73         delete[] _current;
74 }
75
76 string
77 AnalyseAudioJob::name () const
78 {
79         return _("Analyse audio");
80 }
81
82 string
83 AnalyseAudioJob::json_name () const
84 {
85         return N_("analyse_audio");
86 }
87
88 void
89 AnalyseAudioJob::run ()
90 {
91         shared_ptr<Player> player (new Player (_film, _playlist));
92         player->set_ignore_video ();
93         player->set_fast ();
94         player->set_play_referenced ();
95
96         DCPTime const start = _playlist->start().get_value_or (DCPTime ());
97         DCPTime const length = _playlist->length ();
98
99         Frame const len = DCPTime (length - start).frames_round (_film->audio_frame_rate());
100         _samples_per_point = max (int64_t (1), len / _num_points);
101
102         delete[] _current;
103         _current = new AudioPoint[_film->audio_channels ()];
104         _analysis.reset (new AudioAnalysis (_film->audio_channels ()));
105
106         bool has_any_audio = false;
107         BOOST_FOREACH (shared_ptr<Content> c, _playlist->content ()) {
108                 if (dynamic_pointer_cast<AudioContent> (c)) {
109                         has_any_audio = true;
110                 }
111         }
112
113         if (has_any_audio) {
114                 _done = 0;
115                 DCPTime const block = DCPTime::from_seconds (1.0 / 8);
116                 for (DCPTime t = start; t < length; t += block) {
117                         shared_ptr<const AudioBuffers> audio = player->get_audio (t, block, false);
118 #ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
119                         _ebur128->process (audio);
120 #endif
121                         analyse (audio);
122                         set_progress ((t.seconds() - start.seconds()) / (length.seconds() - start.seconds()));
123                 }
124         }
125
126         _analysis->set_sample_peak (_sample_peak, DCPTime::from_frames (_sample_peak_frame, _film->audio_frame_rate ()));
127
128 #ifdef DCPOMATIC_HAVE_PATCHED_FFMPEG
129         void* eb = _ebur128->get("Parsed_ebur128_0")->priv;
130         double true_peak = 0;
131         for (int i = 0; i < _film->audio_channels(); ++i) {
132                 true_peak = max (true_peak, av_ebur128_get_true_peaks(eb)[i]);
133         }
134         _analysis->set_true_peak (true_peak);
135         _analysis->set_integrated_loudness (av_ebur128_get_integrated_loudness(eb));
136         _analysis->set_loudness_range (av_ebur128_get_loudness_range(eb));
137 #endif
138
139         if (_playlist->content().size() == 1) {
140                 /* If there was only one piece of content in this analysis we may later need to know what its
141                    gain was when we analysed it.
142                 */
143                 shared_ptr<const AudioContent> ac = dynamic_pointer_cast<const AudioContent> (_playlist->content().front ());
144                 DCPOMATIC_ASSERT (ac);
145                 _analysis->set_analysis_gain (ac->audio_gain ());
146         }
147
148         _analysis->write (_film->audio_analysis_path (_playlist));
149
150         set_progress (1);
151         set_state (FINISHED_OK);
152 }
153
154 void
155 AnalyseAudioJob::analyse (shared_ptr<const AudioBuffers> b)
156 {
157         int const frames = b->frames ();
158         int const channels = b->channels ();
159
160         for (int j = 0; j < channels; ++j) {
161                 float* data = b->data(j);
162                 for (int i = 0; i < frames; ++i) {
163                         float s = data[i];
164                         float as = fabsf (s);
165                         if (as < 10e-7) {
166                                 /* SafeStringStream can't serialise and recover inf or -inf, so prevent such
167                                    values by replacing with this (140dB down) */
168                                 s = as = 10e-7;
169                         }
170                         _current[j][AudioPoint::RMS] += pow (s, 2);
171                         _current[j][AudioPoint::PEAK] = max (_current[j][AudioPoint::PEAK], as);
172
173                         if (as > _sample_peak) {
174                                 _sample_peak = as;
175                                 _sample_peak_frame = _done + i;
176                         }
177
178                         if (((_done + i) % _samples_per_point) == 0) {
179                                 _current[j][AudioPoint::RMS] = sqrt (_current[j][AudioPoint::RMS] / _samples_per_point);
180                                 _analysis->add_point (j, _current[j]);
181                                 _current[j] = AudioPoint ();
182                         }
183                 }
184         }
185
186         _done += frames;
187 }