5e2c130a50991a0f247e5035f125d819da2a8949
[dcpomatic.git] / test / ffmpeg_audio_only_test.cc
1 /*
2     Copyright (C) 2016-2017 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 /** @file  test/ffmpeg_audio_only_test.cc
22  *  @brief Test FFmpeg content with audio but no video.
23  *  @ingroup feature
24  */
25
26 #include "lib/film.h"
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"
32 #include "test.h"
33 #include <dcp/sound_asset.h>
34 #include <dcp/sound_asset_reader.h>
35 #include <sndfile.h>
36 #include <boost/test/unit_test.hpp>
37 #include <iostream>
38
39 using std::min;
40 #if BOOST_VERSION >= 106100
41 using namespace boost::placeholders;
42 #endif
43 using boost::shared_ptr;
44
45 static SNDFILE* ref = 0;
46 static int ref_buffer_size = 0;
47 static float* ref_buffer = 0;
48
49 static void
50 audio (boost::shared_ptr<AudioBuffers> audio, int channels)
51 {
52         /* Check that we have a big enough buffer */
53         BOOST_CHECK (audio->frames() * audio->channels() < ref_buffer_size);
54
55         int const N = sf_readf_float (ref, ref_buffer, audio->frames());
56         for (int i = 0; i < N; ++i) {
57                 switch (channels) {
58                 case 1:
59                         BOOST_CHECK_EQUAL (ref_buffer[i], audio->data(2)[i]);
60                         break;
61                 case 2:
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]);
64                         break;
65                 default:
66                         BOOST_REQUIRE (false);
67                 }
68         }
69 }
70
71 /** Test the FFmpeg code with audio-only content */
72 static shared_ptr<Film>
73 test (boost::filesystem::path file)
74 {
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 ();
82
83         /* See if can make a DCP without any errors */
84         film->make_dcp ();
85         BOOST_REQUIRE (!wait_for_jobs());
86         BOOST_CHECK (!JobManager::instance()->errors());
87
88         /* Compare the audio data player reads with what libsndfile reads */
89
90         SF_INFO info;
91         info.format = 0;
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];
97
98         shared_ptr<Player> player (new Player(film));
99
100         player->Audio.connect (bind (&audio, _1, info.channels));
101         while (!player->pass ()) {}
102
103         sf_close (ref);
104         delete[] ref_buffer;
105
106         return film;
107 }
108
109 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test1)
110 {
111         /* S16 */
112         shared_ptr<Film> film = test ("test/data/staircase.wav");
113
114         /* Compare the audio data in the DCP with what libsndfile reads */
115
116         SF_INFO info;
117         info.format = 0;
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);
121
122         int16_t* buffer = new int16_t[info.channels * 2000];
123
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]);
132                 }
133                 info.frames -= this_time;
134         }
135
136         delete[] buffer;
137 }
138
139 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test2)
140 {
141         /* S32 1 channel */
142         shared_ptr<Film> film = test ("test/data/sine_440.wav");
143
144         /* Compare the audio data in the DCP with what libsndfile reads */
145
146         SF_INFO info;
147         info.format = 0;
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);
151
152         int32_t* buffer = new int32_t[info.channels * 2000];
153
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);
162                         if (s > (1 << 23)) {
163                                 s -= (1 << 24);
164                         }
165                         BOOST_REQUIRE_MESSAGE (abs(s - (buffer[j] / 256)) <= 1, "failed on asset frame " << i << " sample " << j);
166                 }
167                 info.frames -= this_time;
168         }
169
170         delete[] buffer;
171 }
172
173 BOOST_AUTO_TEST_CASE (ffmpeg_audio_only_test3)
174 {
175         /* S24 1 channel */
176         shared_ptr<Film> film = test ("test/data/sine_24_48_440.wav");
177
178         /* Compare the audio data in the DCP with what libsndfile reads */
179
180         SF_INFO info;
181         info.format = 0;
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);
185
186         int32_t* buffer = new int32_t[info.channels * 2000];
187
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);
196                         if (s > (1 << 23)) {
197                                 s -= (1 << 24);
198                         }
199                         BOOST_REQUIRE_MESSAGE (abs(s - buffer[j] /256) <= 1, "failed on asset frame " << i << " sample " << j);
200                 }
201                 info.frames -= this_time;
202         }
203
204         delete[] buffer;
205 }