Default to writing 8 audio channels in DCPs (#2443).
[dcpomatic.git] / test / threed_test.cc
1 /*
2     Copyright (C) 2013-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/threed_test.cc
23  *  @brief Create some 3D DCPs (without comparing the results to anything).
24  *  @ingroup completedcp
25  */
26
27
28 #include "lib/butler.h"
29 #include "lib/config.h"
30 #include "lib/content_factory.h"
31 #include "lib/cross.h"
32 #include "lib/dcp_content.h"
33 #include "lib/dcp_content_type.h"
34 #include "lib/ffmpeg_content.h"
35 #include "lib/film.h"
36 #include "lib/image.h"
37 #include "lib/job.h"
38 #include "lib/job_manager.h"
39 #include "lib/make_dcp.h"
40 #include "lib/player.h"
41 #include "lib/ratio.h"
42 #include "lib/util.h"
43 #include "lib/video_content.h"
44 #include "test.h"
45 #include <dcp/mono_picture_asset.h>
46 #include <dcp/stereo_picture_asset.h>
47 #include <boost/test/unit_test.hpp>
48 #include <iostream>
49
50
51 using std::cout;
52 using std::make_shared;
53 using std::shared_ptr;
54
55
56 /** Basic sanity check of THREE_D_LEFT_RIGHT */
57 BOOST_AUTO_TEST_CASE (threed_test1)
58 {
59         auto film = new_test_film ("threed_test1");
60         film->set_name ("test_film1");
61         auto c = make_shared<FFmpegContent>("test/data/test.mp4");
62         film->examine_and_add_content (c);
63         BOOST_REQUIRE (!wait_for_jobs());
64
65         c->video->set_frame_type (VideoFrameType::THREE_D_LEFT_RIGHT);
66
67         film->set_container (Ratio::from_id ("185"));
68         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
69         film->set_three_d (true);
70         make_and_verify_dcp (film);
71 }
72
73
74 /** Basic sanity check of THREE_D_ALTERNATE; at the moment this is just to make sure
75  *  that such a transcode completes without error.
76  */
77 BOOST_AUTO_TEST_CASE (threed_test2)
78 {
79         auto film = new_test_film ("threed_test2");
80         film->set_name ("test_film2");
81         auto c = make_shared<FFmpegContent>("test/data/test.mp4");
82         film->examine_and_add_content (c);
83         BOOST_REQUIRE (!wait_for_jobs());
84
85         c->video->set_frame_type (VideoFrameType::THREE_D_ALTERNATE);
86
87         film->set_container (Ratio::from_id ("185"));
88         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("TST"));
89         film->set_three_d (true);
90         make_and_verify_dcp (film);
91 }
92
93
94 /** Basic sanity check of THREE_D_LEFT and THREE_D_RIGHT; at the moment this is just to make sure
95  *  that such a transcode completes without error.
96  */
97 BOOST_AUTO_TEST_CASE (threed_test3)
98 {
99         auto film = new_test_film2 ("threed_test3");
100         auto L = make_shared<FFmpegContent>("test/data/test.mp4");
101         film->examine_and_add_content (L);
102         auto R = make_shared<FFmpegContent>("test/data/test.mp4");
103         film->examine_and_add_content (R);
104         BOOST_REQUIRE (!wait_for_jobs());
105
106         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
107         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
108
109         film->set_three_d (true);
110         make_and_verify_dcp (film);
111 }
112
113
114 BOOST_AUTO_TEST_CASE (threed_test4)
115 {
116         ConfigRestorer cr;
117
118         /* Try to stop out-of-memory crashes on my laptop */
119         Config::instance()->set_master_encoding_threads (boost::thread::hardware_concurrency() / 4);
120
121         auto film = new_test_film2 ("threed_test4");
122         auto L = make_shared<FFmpegContent>(TestPaths::private_data() / "LEFT_TEST_DCP3D4K.mov");
123         film->examine_and_add_content (L);
124         auto R = make_shared<FFmpegContent>(TestPaths::private_data() / "RIGHT_TEST_DCP3D4K.mov");
125         film->examine_and_add_content (R);
126         BOOST_REQUIRE (!wait_for_jobs());
127
128         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
129         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
130         /* There doesn't seem much point in encoding the whole input, especially as we're only
131          * checking for errors during the encode and not the result.  Also decoding these files
132          * (4K HQ Prores) is very slow.
133          */
134         L->set_trim_end (dcpomatic::ContentTime::from_seconds(22));
135         R->set_trim_end (dcpomatic::ContentTime::from_seconds(22));
136
137         film->set_three_d (true);
138         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_PICTURE_ASSET_RESOLUTION_FOR_3D});
139 }
140
141
142 BOOST_AUTO_TEST_CASE (threed_test5)
143 {
144         auto film = new_test_film2 ("threed_test5");
145         auto L = make_shared<FFmpegContent>(TestPaths::private_data() / "boon_telly.mkv");
146         film->examine_and_add_content (L);
147         auto R = make_shared<FFmpegContent>(TestPaths::private_data() / "boon_telly.mkv");
148         film->examine_and_add_content (R);
149         BOOST_REQUIRE (!wait_for_jobs());
150
151         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
152         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
153         /* There doesn't seem much point in encoding the whole input, especially as we're only
154          * checking for errors during the encode and not the result.
155          */
156         L->set_trim_end (dcpomatic::ContentTime::from_seconds(3 * 60 + 20));
157         R->set_trim_end (dcpomatic::ContentTime::from_seconds(3 * 60 + 20));
158
159         film->set_three_d (true);
160         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_PICTURE_FRAME_RATE_FOR_2K});
161 }
162
163
164 BOOST_AUTO_TEST_CASE (threed_test6)
165 {
166         auto film = new_test_film2 ("threed_test6");
167         auto L = make_shared<FFmpegContent>("test/data/3dL.mp4");
168         film->examine_and_add_content (L);
169         auto R = make_shared<FFmpegContent>("test/data/3dR.mp4");
170         film->examine_and_add_content (R);
171         film->set_audio_channels(16);
172         BOOST_REQUIRE (!wait_for_jobs());
173
174         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
175         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
176
177         film->set_three_d (true);
178         make_and_verify_dcp (film);
179         check_dcp ("test/data/threed_test6", film->dir(film->dcp_name()));
180 }
181
182
183 /** Check 2D content set as being 3D; this should give an informative error */
184 BOOST_AUTO_TEST_CASE (threed_test7)
185 {
186         using boost::filesystem::path;
187
188         auto film = new_test_film2 ("threed_test7");
189         path const content_path = "test/data/flat_red.png";
190         auto c = content_factory(content_path)[0];
191         film->examine_and_add_content (c);
192         BOOST_REQUIRE (!wait_for_jobs());
193
194         c->video->set_frame_type (VideoFrameType::THREE_D);
195         c->video->set_length (24);
196
197         film->set_three_d (true);
198         make_dcp (film, TranscodeJob::ChangedBehaviour::IGNORE);
199         film->write_metadata ();
200
201         auto jm = JobManager::instance ();
202         while (jm->work_to_do ()) {
203                 while (signal_manager->ui_idle()) {}
204                 dcpomatic_sleep_seconds (1);
205         }
206
207         while (signal_manager->ui_idle ()) {}
208
209         BOOST_REQUIRE (jm->errors());
210         shared_ptr<Job> failed;
211         for (auto i: jm->_jobs) {
212                 if (i->finished_in_error()) {
213                         BOOST_REQUIRE (!failed);
214                         failed = i;
215                 }
216         }
217         BOOST_REQUIRE (failed);
218         BOOST_CHECK_EQUAL (failed->error_summary(), String::compose("The content file %1 is set as 3D but does not appear to contain 3D images.  Please set it to 2D.  You can still make a 3D DCP from this content by ticking the 3D option in the DCP video tab.", content_path.string()));
219
220         while (signal_manager->ui_idle ()) {}
221
222         JobManager::drop ();
223 }
224
225
226 /** Trigger a -114 error by trying to make a 3D DCP out of two files with slightly
227  *  different lengths.
228  */
229 BOOST_AUTO_TEST_CASE (threed_test_separate_files_slightly_different_lengths)
230 {
231         auto film = new_test_film2("threed_test3");
232         auto L = make_shared<FFmpegContent>("test/data/test.mp4");
233         film->examine_and_add_content (L);
234         auto R = make_shared<FFmpegContent>("test/data/test.mp4");
235         film->examine_and_add_content (R);
236         BOOST_REQUIRE (!wait_for_jobs());
237
238         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
239         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
240         R->set_trim_end (dcpomatic::ContentTime::from_frames(1, 24));
241
242         film->set_three_d (true);
243         make_and_verify_dcp (film);
244 }
245
246
247 /** Trigger a -114 error by trying to make a 3D DCP out of two files with very
248  *  different lengths.
249  */
250 BOOST_AUTO_TEST_CASE (threed_test_separate_files_very_different_lengths)
251 {
252         auto film = new_test_film2("threed_test3");
253         auto L = make_shared<FFmpegContent>("test/data/test.mp4");
254         film->examine_and_add_content (L);
255         auto R = make_shared<FFmpegContent>("test/data/test.mp4");
256         film->examine_and_add_content (R);
257         BOOST_REQUIRE (!wait_for_jobs());
258
259         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
260         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
261         R->set_trim_end (dcpomatic::ContentTime::from_seconds(1.5));
262
263         film->set_three_d (true);
264         make_and_verify_dcp (film);
265 }
266
267
268 BOOST_AUTO_TEST_CASE (threed_test_butler_overfill)
269 {
270         auto film = new_test_film2("threed_test_butler_overfill");
271         auto A = make_shared<FFmpegContent>(TestPaths::private_data() / "arrietty_JP-EN.mkv");
272         film->examine_and_add_content(A);
273         auto B = make_shared<FFmpegContent>(TestPaths::private_data() / "arrietty_JP-EN.mkv");
274         film->examine_and_add_content(B);
275         BOOST_REQUIRE (!wait_for_jobs());
276
277         Player player(film, Image::Alignment::COMPACT);
278         int const audio_channels = 2;
279         auto butler = std::make_shared<Butler>(
280                 film, player, AudioMapping(), audio_channels, boost::bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::ENABLED
281                 );
282
283         int const audio_frames = 1920;
284         std::vector<float> audio(audio_frames * audio_channels);
285
286         B->video->set_frame_type(VideoFrameType::THREE_D_RIGHT);
287         B->set_position(film, dcpomatic::DCPTime());
288
289         butler->seek(dcpomatic::DCPTime(), true);
290         Butler::Error error;
291         for (auto i = 0; i < 960; ++i) {
292                 butler->get_video(Butler::Behaviour::BLOCKING, &error);
293                 butler->get_audio(Butler::Behaviour::BLOCKING, audio.data(), audio_frames);
294         }
295         BOOST_REQUIRE (error.code == Butler::Error::Code::NONE);
296 }
297
298
299 /** Check that creating a 2D DCP from a 3D DCP passes the J2K data unaltered */
300 BOOST_AUTO_TEST_CASE(threed_passthrough_test, * boost::unit_test::depends_on("threed_test6"))
301 {
302         using namespace boost::filesystem;
303
304         /* Find the DCP in threed_test6 */
305         boost::optional<path> input_dcp;
306         for (auto i: directory_iterator("build/test/threed_test6")) {
307                 if (is_directory(i.path()) && boost::algorithm::starts_with(i.path().filename().string(), "Dcp")) {
308                         input_dcp = i.path();
309                 }
310         }
311
312         BOOST_REQUIRE(input_dcp);
313
314         auto content = make_shared<DCPContent>(*input_dcp);
315         auto film = new_test_film2("threed_passthrough_test", { content });
316         film->set_three_d(false);
317
318         make_and_verify_dcp(film);
319
320         std::vector<directory_entry> matches;
321         std::copy_if(recursive_directory_iterator(*input_dcp), recursive_directory_iterator(), std::back_inserter(matches), [](directory_entry const& entry) {
322                 return boost::algorithm::starts_with(entry.path().leaf().string(), "j2c");
323         });
324
325         BOOST_REQUIRE_EQUAL(matches.size(), 1U);
326
327         auto stereo = dcp::StereoPictureAsset(matches[0]);
328         auto stereo_reader = stereo.start_read();
329
330         auto mono = dcp::MonoPictureAsset(dcp_file(film, "j2c"));
331         auto mono_reader = mono.start_read();
332
333         BOOST_REQUIRE_EQUAL(stereo.intrinsic_duration(), mono.intrinsic_duration());
334
335         for (auto i = 0; i < stereo.intrinsic_duration(); ++i) {
336                 auto stereo_frame = stereo_reader->get_frame(i);
337                 auto mono_frame = mono_reader->get_frame(i);
338                 BOOST_REQUIRE(stereo_frame->left()->size() == mono_frame->size());
339                 BOOST_REQUIRE_EQUAL(memcmp(stereo_frame->left()->data(), mono_frame->data(), mono_frame->size()), 0);
340         }
341 }
342
343 /* #2476 was a writer error when 3D picture padding is needed */
344 BOOST_AUTO_TEST_CASE(threed_test_when_padding_needed)
345 {
346         auto left = content_factory("test/data/flat_red.png").front();
347         auto right = content_factory("test/data/flat_red.png").front();
348         auto sound = content_factory("test/data/sine_440.wav").front();
349         auto film = new_test_film2("threed_test_when_padding_needed", { left, right, sound });
350
351         left->video->set_frame_type(VideoFrameType::THREE_D_LEFT);
352         left->set_position(film, dcpomatic::DCPTime());
353         left->video->set_length(23);
354         right->video->set_frame_type(VideoFrameType::THREE_D_RIGHT);
355         right->set_position(film, dcpomatic::DCPTime());
356         right->video->set_frame_type(VideoFrameType::THREE_D_RIGHT);
357         right->video->set_length(23);
358         film->set_three_d(true);
359
360         make_and_verify_dcp(film);
361 }