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