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