2 Copyright (C) 2016-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 /** @file test/ffmpeg_audio_only_test.cc
23 * @brief Test FFmpeg content with audio but no video.
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"
35 #include <dcp/sound_asset.h>
36 #include <dcp/sound_asset_reader.h>
38 #include <boost/test/unit_test.hpp>
43 using std::shared_ptr;
44 using std::make_shared;
45 #if BOOST_VERSION >= 106100
46 using namespace boost::placeholders;
50 static SNDFILE* ref = nullptr;
51 static int ref_buffer_size = 0;
52 static float* ref_buffer = nullptr;
56 audio (std::shared_ptr<AudioBuffers> audio, int channels)
58 /* Check that we have a big enough buffer */
59 BOOST_CHECK (audio->frames() * audio->channels() < ref_buffer_size);
61 int const N = sf_readf_float (ref, ref_buffer, audio->frames());
62 for (int i = 0; i < N; ++i) {
65 BOOST_CHECK_EQUAL (ref_buffer[i], audio->data(2)[i]);
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]);
72 BOOST_REQUIRE (false);
78 /** Test the FFmpeg code with audio-only content */
79 static shared_ptr<Film>
80 test (boost::filesystem::path file)
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 ();
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());
94 /* Compare the audio data player reads with what libsndfile reads */
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];
104 auto player = make_shared<Player>(film, Image::Alignment::COMPACT);
106 player->Audio.connect (bind (&audio, _1, info.channels));
107 while (!player->pass ()) {}
116 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test1)
119 auto film = test ("test/data/staircase.wav");
121 /* Compare the audio data in the DCP with what libsndfile reads */
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);
129 auto buffer = new int16_t[info.channels * 2000];
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]);
140 info.frames -= this_time;
147 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test2)
150 auto film = test ("test/data/sine_440.wav");
152 /* Compare the audio data in the DCP with what libsndfile reads */
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);
160 auto buffer = new int32_t[info.channels * 2000];
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);
173 BOOST_REQUIRE_MESSAGE (abs(s - (buffer[j] / 256)) <= 1, "failed on asset frame " << i << " sample " << j);
175 info.frames -= this_time;
182 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test3)
185 auto film = test ("test/data/sine_24_48_440.wav");
187 /* Compare the audio data in the DCP with what libsndfile reads */
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);
195 auto buffer = new int32_t[info.channels * 2000];
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);
208 BOOST_REQUIRE_MESSAGE (abs(s - buffer[j] /256) <= 1, "failed on asset frame " << i << " sample " << j);
210 info.frames -= this_time;