Supporters update.
[dcpomatic.git] / test / audio_analysis_test.cc
1 /*
2     Copyright (C) 2012-2021 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
22 /** @defgroup selfcontained Self-contained tests of single classes / method sets */
23
24 /** @file  test/audio_analysis_test.cc
25  *  @brief Test AudioAnalysis class.
26  *  @ingroup selfcontained
27  */
28
29
30 #include "lib/analyse_audio_job.h"
31 #include "lib/audio_analysis.h"
32 #include "lib/audio_content.h"
33 #include "lib/content_factory.h"
34 #include "lib/dcp_content.h"
35 #include "lib/dcp_content_type.h"
36 #include "lib/ffmpeg_content.h"
37 #include "lib/ffmpeg_content.h"
38 #include "lib/film.h"
39 #include "lib/job_manager.h"
40 #include "lib/playlist.h"
41 #include "lib/ratio.h"
42 #include "test.h"
43 #include <boost/test/unit_test.hpp>
44 #include <numeric>
45
46
47 using std::make_shared;
48 using std::vector;
49 using namespace dcpomatic;
50
51
52 BOOST_AUTO_TEST_CASE (audio_analysis_serialisation_test)
53 {
54         int const channels = 3;
55         int const points = 4096;
56
57         auto random_float = []() {
58                 return (float (rand ()) / RAND_MAX) * 2 - 1;
59         };
60
61         AudioAnalysis a (3);
62         for (int i = 0; i < channels; ++i) {
63                 for (int j = 0; j < points; ++j) {
64                         AudioPoint p;
65                         p[AudioPoint::PEAK] = random_float ();
66                         p[AudioPoint::RMS] = random_float ();
67                         a.add_point (i, p);
68                 }
69         }
70
71         vector<AudioAnalysis::PeakTime> peak;
72         for (int i = 0; i < channels; ++i) {
73                 peak.push_back (AudioAnalysis::PeakTime(random_float(), DCPTime(rand())));
74         }
75         a.set_sample_peak (peak);
76
77         a.set_samples_per_point (100);
78         a.set_sample_rate (48000);
79         a.write ("build/test/audio_analysis_serialisation_test");
80
81         AudioAnalysis b ("build/test/audio_analysis_serialisation_test");
82         for (int i = 0; i < channels; ++i) {
83                 BOOST_CHECK_EQUAL (b.points(i), points);
84                 for (int j = 0; j < points; ++j) {
85                         AudioPoint p = a.get_point (i, j);
86                         AudioPoint q = b.get_point (i, j);
87                         BOOST_CHECK_CLOSE (p[AudioPoint::PEAK], q[AudioPoint::PEAK], 1);
88                         BOOST_CHECK_CLOSE (p[AudioPoint::RMS],  q[AudioPoint::RMS], 1);
89                 }
90         }
91
92         BOOST_REQUIRE_EQUAL (b.sample_peak().size(), 3U);
93         for (int i = 0; i < channels; ++i) {
94                 BOOST_CHECK_CLOSE (b.sample_peak()[i].peak, peak[i].peak, 1);
95                 BOOST_CHECK_EQUAL (b.sample_peak()[i].time.get(), peak[i].time.get());
96         }
97
98         BOOST_CHECK_EQUAL (a.samples_per_point(), 100);
99         BOOST_CHECK_EQUAL (a.sample_rate(), 48000);
100 }
101
102
103 BOOST_AUTO_TEST_CASE (audio_analysis_test)
104 {
105         auto film = new_test_film ("audio_analysis_test");
106         film->set_dcp_content_type (DCPContentType::from_isdcf_name("FTR"));
107         film->set_container (Ratio::from_id("185"));
108         film->set_name ("audio_analysis_test");
109         boost::filesystem::path p = TestPaths::private_data() / "betty_L.wav";
110
111         auto c = make_shared<FFmpegContent>(p);
112         film->examine_and_add_content (c);
113         BOOST_REQUIRE (!wait_for_jobs());
114
115         auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), false);
116         JobManager::instance()->add (job);
117         BOOST_REQUIRE (!wait_for_jobs());
118 }
119
120
121 /** Check that audio analysis works (i.e. runs without error) with a -ve delay */
122 BOOST_AUTO_TEST_CASE (audio_analysis_negative_delay_test)
123 {
124         auto film = new_test_film ("audio_analysis_negative_delay_test");
125         film->set_name ("audio_analysis_negative_delay_test");
126         auto c = make_shared<FFmpegContent>(TestPaths::private_data() / "boon_telly.mkv");
127         film->examine_and_add_content (c);
128         BOOST_REQUIRE (!wait_for_jobs());
129
130         c->audio->set_delay (-250);
131
132         auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), false);
133         JobManager::instance()->add (job);
134         BOOST_REQUIRE (!wait_for_jobs());
135 }
136
137
138 /** Check audio analysis that is incorrect in 2e98263 */
139 BOOST_AUTO_TEST_CASE (audio_analysis_test2)
140 {
141         auto film = new_test_film ("audio_analysis_test2");
142         film->set_name ("audio_analysis_test2");
143         auto c = make_shared<FFmpegContent>(TestPaths::private_data() / "3d_thx_broadway_2010_lossless.m2ts");
144         film->examine_and_add_content (c);
145         BOOST_REQUIRE (!wait_for_jobs());
146
147         auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), false);
148         JobManager::instance()->add (job);
149         BOOST_REQUIRE (!wait_for_jobs());
150 }
151
152
153 /* Test a case which was reported to throw an exception; analysing
154  * a 12-channel DCP's audio.
155  */
156 BOOST_AUTO_TEST_CASE (audio_analysis_test3)
157 {
158         auto film = new_test_film ("analyse_audio_test");
159         film->set_container (Ratio::from_id ("185"));
160         film->set_dcp_content_type (DCPContentType::from_isdcf_name("TLR"));
161         film->set_name ("frobozz");
162
163         auto content = make_shared<FFmpegContent>("test/data/white.wav");
164         film->examine_and_add_content (content);
165         BOOST_REQUIRE (!wait_for_jobs());
166
167         film->set_audio_channels (12);
168         boost::signals2::connection connection;
169         bool done = false;
170         JobManager::instance()->analyse_audio(film, film->playlist(), false, connection, [&done](Job::Result) { done = true; });
171         BOOST_REQUIRE (!wait_for_jobs());
172         BOOST_CHECK (done);
173 }
174
175
176 /** Run an audio analysis that triggered an exception in the audio decoder at one point */
177 BOOST_AUTO_TEST_CASE (analyse_audio_test4)
178 {
179         auto film = new_test_film ("analyse_audio_test");
180         film->set_container (Ratio::from_id ("185"));
181         film->set_dcp_content_type (DCPContentType::from_isdcf_name("TLR"));
182         film->set_name ("frobozz");
183         auto content = content_factory(TestPaths::private_data() / "20 The Wedding Convoy Song.m4a")[0];
184         film->examine_and_add_content (content);
185         BOOST_REQUIRE (!wait_for_jobs());
186
187         auto playlist = make_shared<Playlist>();
188         playlist->add (film, content);
189         boost::signals2::connection c;
190         JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
191         BOOST_CHECK (!wait_for_jobs ());
192 }
193
194
195 BOOST_AUTO_TEST_CASE (analyse_audio_leqm_test)
196 {
197         auto film = new_test_film2 ("analyse_audio_leqm_test");
198         film->set_audio_channels (2);
199         auto content = content_factory(TestPaths::private_data() / "betty_stereo_48k.wav")[0];
200         film->examine_and_add_content (content);
201         BOOST_REQUIRE (!wait_for_jobs());
202
203         auto playlist = make_shared<Playlist>();
204         playlist->add (film, content);
205         boost::signals2::connection c;
206         JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
207         BOOST_CHECK (!wait_for_jobs());
208
209         AudioAnalysis analysis(film->audio_analysis_path(playlist));
210
211         /* The CLI tool of leqm_nrt gives this value for betty_stereo_48k.wav */
212         BOOST_CHECK_CLOSE (analysis.leqm().get_value_or(0), 88.276, 0.001);
213 }
214
215
216 BOOST_AUTO_TEST_CASE(analyse_audio_leqm_same_with_empty_channels)
217 {
218         auto dcp = make_shared<DCPContent>(TestPaths::private_data() / "JourneyToJah_TLR-1_F_EN-DE-FR_CH_51_2K_LOK_20140225_DGL_SMPTE_OV");
219         auto film = new_test_film2("analyse_audio_leqm_test2", { dcp });
220         film->set_audio_channels(8);
221
222         auto analyse = [film, dcp](int channels) {
223                 film->set_audio_channels(channels);
224                 auto playlist = make_shared<Playlist>();
225                 playlist->add(film, dcp);
226                 boost::signals2::connection c;
227                 JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
228                 BOOST_CHECK(!wait_for_jobs());
229                 AudioAnalysis analysis(film->audio_analysis_path(playlist));
230                 return analysis.leqm().get_value_or(0);
231         };
232
233         BOOST_CHECK_CLOSE(analyse( 6), 84.51411, 0.001);
234         BOOST_CHECK_CLOSE(analyse( 8), 84.51411, 0.001);
235         BOOST_CHECK_CLOSE(analyse(16), 84.51411, 0.001);
236 }
237
238
239 /** Bug #2364; a file with a lot of silent video at the end (about 50s worth)
240  *  crashed the audio analysis with an OOM on Windows.
241  */
242 BOOST_AUTO_TEST_CASE(analyse_audio_with_long_silent_end)
243 {
244         auto content = content_factory(TestPaths::private_data() / "2364.mkv")[0];
245         auto film = new_test_film2("analyse_audio_with_long_silent_end", { content });
246
247         auto playlist = make_shared<Playlist>();
248         playlist->add(film, content);
249         boost::signals2::connection c;
250         JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
251         BOOST_CHECK(!wait_for_jobs());
252 }
253
254
255 BOOST_AUTO_TEST_CASE(analyse_audio_with_strange_channel_count)
256 {
257         auto content = content_factory(TestPaths::private_data() / "mali.mkv")[0];
258         auto film = new_test_film2("analyse_audio_with_strange_channel_count", { content });
259
260         auto playlist = make_shared<Playlist>();
261         playlist->add(film, content);
262         boost::signals2::connection c;
263         JobManager::instance()->analyse_audio(film, playlist, false, c, [](Job::Result) {});
264         BOOST_CHECK(!wait_for_jobs());
265 }
266
267
268 BOOST_AUTO_TEST_CASE(analyse_audio_with_more_channels_than_film)
269 {
270         auto picture = content_factory("test/data/flat_red.png");
271         auto film_16ch = new_test_film2("analyse_audio_with_more_channels_than_film_16ch", picture);
272         film_16ch->set_audio_channels(16);
273         make_and_verify_dcp(film_16ch);
274
275         auto pcm_16ch = find_file(film_16ch->dir(film_16ch->dcp_name()), "pcm_");
276         auto sound = content_factory(pcm_16ch)[0];
277
278         auto film_6ch = new_test_film2("analyse_audio_with_more_channels_than_film_6ch", { sound });
279
280         auto playlist = make_shared<Playlist>();
281         playlist->add(film_6ch, sound);
282         boost::signals2::connection c;
283         JobManager::instance()->analyse_audio(film_6ch, playlist, false, c, [](Job::Result) {});
284         BOOST_CHECK(!wait_for_jobs());
285 }
286
287
288 BOOST_AUTO_TEST_CASE(analyse_audio_uses_processor_when_analysing_whole_film)
289 {
290         auto sound = content_factory(TestPaths::private_data() / "betty_stereo.wav")[0];
291         auto film = new_test_film2("analyse_audio_uses_processor_when_analysing_whole_film", { sound });
292
293         auto job = make_shared<AnalyseAudioJob>(film, film->playlist(), true);
294         JobManager::instance()->add(job);
295         BOOST_REQUIRE(!wait_for_jobs());
296
297         AudioAnalysis analysis(job->path());
298
299         BOOST_REQUIRE(analysis.channels() > 2);
300         bool centre_non_zero = false;
301         /* Make sure there's something from the mid-side decoder on the centre channel */
302         for (auto point = 0; point < analysis.points(2); ++point) {
303                 if (std::abs(analysis.get_point(2, point)[AudioPoint::Type::PEAK]) > 0) {
304                         centre_non_zero = true;
305                 }
306         }
307
308         BOOST_CHECK(centre_non_zero);
309 }
310