Fix typo if -> of (thanks to Uwe Dittes)
[dcpomatic.git] / test / ffmpeg_audio_only_test.cc
1 /*
2     Copyright (C) 2016-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 /** @file  test/ffmpeg_audio_only_test.cc
23  *  @brief Test FFmpeg content with audio but no video.
24  *  @ingroup feature
25  */
26
27
28 #include "lib/film.h"
29 #include "lib/ffmpeg_content.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/player.h"
32 #include "lib/job_manager.h"
33 #include "lib/audio_buffers.h"
34 #include "test.h"
35 #include <dcp/sound_asset.h>
36 #include <dcp/sound_asset_reader.h>
37 #include <sndfile.h>
38 #include <boost/test/unit_test.hpp>
39 #include <iostream>
40
41
42 using std::min;
43 using std::shared_ptr;
44 using std::make_shared;
45 #if BOOST_VERSION >= 106100
46 using namespace boost::placeholders;
47 #endif
48
49
50 static SNDFILE* ref = nullptr;
51 static int ref_buffer_size = 0;
52 static float* ref_buffer = nullptr;
53
54
55 static void
56 audio (std::shared_ptr<AudioBuffers> audio, int channels)
57 {
58         /* Check that we have a big enough buffer */
59         BOOST_CHECK (audio->frames() * audio->channels() < ref_buffer_size);
60
61         int const N = sf_readf_float (ref, ref_buffer, audio->frames());
62         for (int i = 0; i < N; ++i) {
63                 switch (channels) {
64                 case 1:
65                         BOOST_CHECK_EQUAL (ref_buffer[i], audio->data(2)[i]);
66                         break;
67                 case 2:
68                         BOOST_CHECK_EQUAL (ref_buffer[i*2 + 0], audio->data(0)[i]);
69                         BOOST_CHECK_EQUAL (ref_buffer[i*2 + 1], audio->data(1)[i]);
70                         break;
71                 default:
72                         BOOST_REQUIRE (false);
73                 }
74         }
75 }
76
77
78 /** Test the FFmpeg code with audio-only content */
79 static shared_ptr<Film>
80 test (boost::filesystem::path file)
81 {
82         auto film = new_test_film ("ffmpeg_audio_only_test");
83         film->set_name ("test_film");
84         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
85         auto c = make_shared<FFmpegContent>(file);
86         film->examine_and_add_content (c);
87         BOOST_REQUIRE (!wait_for_jobs());
88         film->write_metadata ();
89
90         /* See if can make a DCP without any errors */
91         make_and_verify_dcp (film, {dcp::VerificationNote::Code::MISSING_CPL_METADATA});
92         BOOST_CHECK (!JobManager::instance()->errors());
93
94         /* Compare the audio data player reads with what libsndfile reads */
95
96         SF_INFO info;
97         info.format = 0;
98         ref = sf_open (file.string().c_str(), SFM_READ, &info);
99         /* We don't want to test anything that requires resampling */
100         BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
101         ref_buffer_size = info.samplerate * info.channels;
102         ref_buffer = new float[ref_buffer_size];
103
104         auto player = make_shared<Player>(film);
105
106         player->Audio.connect (bind (&audio, _1, info.channels));
107         while (!player->pass ()) {}
108
109         sf_close (ref);
110         delete[] ref_buffer;
111
112         return film;
113 }
114
115
116 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test1)
117 {
118         /* S16 */
119         auto film = test ("test/data/staircase.wav");
120
121         /* Compare the audio data in the DCP with what libsndfile reads */
122
123         SF_INFO info;
124         info.format = 0;
125         ref = sf_open ("test/data/staircase.wav", SFM_READ, &info);
126         /* We don't want to test anything that requires resampling */
127         BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
128
129         auto buffer = new int16_t[info.channels * 2000];
130
131         dcp::SoundAsset asset (dcp_file(film, "pcm"));
132         auto reader = asset.start_read ();
133         for (int i = 0; i < asset.intrinsic_duration(); ++i) {
134                 auto frame = reader->get_frame(i);
135                 sf_count_t this_time = min (info.frames, sf_count_t(2000));
136                 sf_readf_short (ref, buffer, this_time);
137                 for (int j = 0; j < this_time; ++j) {
138                         BOOST_REQUIRE_EQUAL (frame->get(2, j) >> 8, buffer[j]);
139                 }
140                 info.frames -= this_time;
141         }
142
143         delete[] buffer;
144 }
145
146
147 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test2)
148 {
149         /* S32 1 channel */
150         auto film = test ("test/data/sine_440.wav");
151
152         /* Compare the audio data in the DCP with what libsndfile reads */
153
154         SF_INFO info;
155         info.format = 0;
156         ref = sf_open ("test/data/sine_440.wav", SFM_READ, &info);
157         /* We don't want to test anything that requires resampling */
158         BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
159
160         auto buffer = new int32_t[info.channels * 2000];
161
162         dcp::SoundAsset asset (dcp_file(film, "pcm"));
163         auto reader = asset.start_read ();
164         for (int i = 0; i < asset.intrinsic_duration(); ++i) {
165                 auto frame = reader->get_frame(i);
166                 sf_count_t this_time = min (info.frames, sf_count_t(2000));
167                 sf_readf_int (ref, buffer, this_time);
168                 for (int j = 0; j < this_time; ++j) {
169                         int32_t s = frame->get(2, j);
170                         if (s > (1 << 23)) {
171                                 s -= (1 << 24);
172                         }
173                         BOOST_REQUIRE_MESSAGE (abs(s - (buffer[j] / 256)) <= 1, "failed on asset frame " << i << " sample " << j);
174                 }
175                 info.frames -= this_time;
176         }
177
178         delete[] buffer;
179 }
180
181
182 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test3)
183 {
184         /* S24 1 channel */
185         auto film = test ("test/data/sine_24_48_440.wav");
186
187         /* Compare the audio data in the DCP with what libsndfile reads */
188
189         SF_INFO info;
190         info.format = 0;
191         ref = sf_open ("test/data/sine_24_48_440.wav", SFM_READ, &info);
192         /* We don't want to test anything that requires resampling */
193         BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
194
195         auto buffer = new int32_t[info.channels * 2000];
196
197         dcp::SoundAsset asset (dcp_file(film, "pcm"));
198         auto reader = asset.start_read ();
199         for (int i = 0; i < asset.intrinsic_duration(); ++i) {
200                 auto frame = reader->get_frame(i);
201                 sf_count_t this_time = min (info.frames, sf_count_t(2000));
202                 sf_readf_int (ref, buffer, this_time);
203                 for (int j = 0; j < this_time; ++j) {
204                         int32_t s = frame->get(2, j);
205                         if (s > (1 << 23)) {
206                                 s -= (1 << 24);
207                         }
208                         BOOST_REQUIRE_MESSAGE (abs(s - buffer[j] /256) <= 1, "failed on asset frame " << i << " sample " << j);
209                 }
210                 info.frames -= this_time;
211         }
212
213         delete[] buffer;
214 }