2 Copyright (C) 2016-2017 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/>.
21 /** @file test/ffmpeg_audio_only_test.cc
22 * @brief Test FFmpeg content with audio but no video.
27 #include "lib/ffmpeg_content.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/player.h"
30 #include "lib/job_manager.h"
31 #include "lib/audio_buffers.h"
33 #include <dcp/sound_asset.h>
34 #include <dcp/sound_asset_reader.h>
36 #include <boost/test/unit_test.hpp>
40 #if BOOST_VERSION >= 106100
41 using namespace boost::placeholders;
43 using boost::shared_ptr;
45 static SNDFILE* ref = 0;
46 static int ref_buffer_size = 0;
47 static float* ref_buffer = 0;
50 audio (boost::shared_ptr<AudioBuffers> audio, int channels)
52 /* Check that we have a big enough buffer */
53 BOOST_CHECK (audio->frames() * audio->channels() < ref_buffer_size);
55 int const N = sf_readf_float (ref, ref_buffer, audio->frames());
56 for (int i = 0; i < N; ++i) {
59 BOOST_CHECK_EQUAL (ref_buffer[i], audio->data(2)[i]);
62 BOOST_CHECK_EQUAL (ref_buffer[i*2 + 0], audio->data(0)[i]);
63 BOOST_CHECK_EQUAL (ref_buffer[i*2 + 1], audio->data(1)[i]);
66 BOOST_REQUIRE (false);
71 /** Test the FFmpeg code with audio-only content */
72 static shared_ptr<Film>
73 test (boost::filesystem::path file)
75 shared_ptr<Film> film = new_test_film ("ffmpeg_audio_only_test");
76 film->set_name ("test_film");
77 film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
78 shared_ptr<FFmpegContent> c (new FFmpegContent(file));
79 film->examine_and_add_content (c);
80 BOOST_REQUIRE (!wait_for_jobs());
81 film->write_metadata ();
83 /* See if can make a DCP without any errors */
85 BOOST_REQUIRE (!wait_for_jobs());
86 BOOST_CHECK (!JobManager::instance()->errors());
88 /* Compare the audio data player reads with what libsndfile reads */
92 ref = sf_open (file.string().c_str(), SFM_READ, &info);
93 /* We don't want to test anything that requires resampling */
94 BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
95 ref_buffer_size = info.samplerate * info.channels;
96 ref_buffer = new float[ref_buffer_size];
98 shared_ptr<Player> player (new Player(film));
100 player->Audio.connect (bind (&audio, _1, info.channels));
101 while (!player->pass ()) {}
109 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test1)
112 shared_ptr<Film> film = test ("test/data/staircase.wav");
114 /* Compare the audio data in the DCP with what libsndfile reads */
118 ref = sf_open ("test/data/staircase.wav", SFM_READ, &info);
119 /* We don't want to test anything that requires resampling */
120 BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
122 int16_t* buffer = new int16_t[info.channels * 2000];
124 dcp::SoundAsset asset (dcp_file(film, "pcm"));
125 shared_ptr<dcp::SoundAssetReader> reader = asset.start_read ();
126 for (int i = 0; i < asset.intrinsic_duration(); ++i) {
127 shared_ptr<const dcp::SoundFrame> frame = reader->get_frame(i);
128 sf_count_t this_time = min (info.frames, sf_count_t(2000));
129 sf_readf_short (ref, buffer, this_time);
130 for (int j = 0; j < this_time; ++j) {
131 BOOST_REQUIRE_EQUAL (frame->get(2, j) >> 8, buffer[j]);
133 info.frames -= this_time;
139 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test2)
142 shared_ptr<Film> film = test ("test/data/sine_440.wav");
144 /* Compare the audio data in the DCP with what libsndfile reads */
148 ref = sf_open ("test/data/sine_440.wav", SFM_READ, &info);
149 /* We don't want to test anything that requires resampling */
150 BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
152 int32_t* buffer = new int32_t[info.channels * 2000];
154 dcp::SoundAsset asset (dcp_file(film, "pcm"));
155 shared_ptr<dcp::SoundAssetReader> reader = asset.start_read ();
156 for (int i = 0; i < asset.intrinsic_duration(); ++i) {
157 shared_ptr<const dcp::SoundFrame> frame = reader->get_frame(i);
158 sf_count_t this_time = min (info.frames, sf_count_t(2000));
159 sf_readf_int (ref, buffer, this_time);
160 for (int j = 0; j < this_time; ++j) {
161 int32_t s = frame->get(2, j);
165 BOOST_REQUIRE_MESSAGE (abs(s - (buffer[j] / 256)) <= 1, "failed on asset frame " << i << " sample " << j);
167 info.frames -= this_time;
173 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test3)
176 shared_ptr<Film> film = test ("test/data/sine_24_48_440.wav");
178 /* Compare the audio data in the DCP with what libsndfile reads */
182 ref = sf_open ("test/data/sine_24_48_440.wav", SFM_READ, &info);
183 /* We don't want to test anything that requires resampling */
184 BOOST_REQUIRE_EQUAL (info.samplerate, 48000);
186 int32_t* buffer = new int32_t[info.channels * 2000];
188 dcp::SoundAsset asset (dcp_file(film, "pcm"));
189 shared_ptr<dcp::SoundAssetReader> reader = asset.start_read ();
190 for (int i = 0; i < asset.intrinsic_duration(); ++i) {
191 shared_ptr<const dcp::SoundFrame> frame = reader->get_frame(i);
192 sf_count_t this_time = min (info.frames, sf_count_t(2000));
193 sf_readf_int (ref, buffer, this_time);
194 for (int j = 0; j < this_time; ++j) {
195 int32_t s = frame->get(2, j);
199 BOOST_REQUIRE_MESSAGE (abs(s - buffer[j] /256) <= 1, "failed on asset frame " << i << " sample " << j);
201 info.frames -= this_time;