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