Missed update to private test repo version.
[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(
139                 film,
140                 {dcp::VerificationNote::Code::INVALID_PICTURE_ASSET_RESOLUTION_FOR_3D},
141                 true,
142                 /* XXX: Clairmeta error about invalid edit rate 24 for 4K 3D */
143                 false
144                 );
145 }
146
147
148 BOOST_AUTO_TEST_CASE (threed_test5)
149 {
150         auto film = new_test_film2 ("threed_test5");
151         auto L = make_shared<FFmpegContent>(TestPaths::private_data() / "boon_telly.mkv");
152         film->examine_and_add_content (L);
153         auto R = make_shared<FFmpegContent>(TestPaths::private_data() / "boon_telly.mkv");
154         film->examine_and_add_content (R);
155         BOOST_REQUIRE (!wait_for_jobs());
156
157         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
158         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
159         /* There doesn't seem much point in encoding the whole input, especially as we're only
160          * checking for errors during the encode and not the result.
161          */
162         L->set_trim_end (dcpomatic::ContentTime::from_seconds(3 * 60 + 20));
163         R->set_trim_end (dcpomatic::ContentTime::from_seconds(3 * 60 + 20));
164
165         film->set_three_d (true);
166         make_and_verify_dcp (film, {dcp::VerificationNote::Code::INVALID_PICTURE_FRAME_RATE_FOR_2K});
167 }
168
169
170 BOOST_AUTO_TEST_CASE (threed_test6)
171 {
172         auto film = new_test_film2 ("threed_test6");
173         auto L = make_shared<FFmpegContent>("test/data/3dL.mp4");
174         film->examine_and_add_content (L);
175         auto R = make_shared<FFmpegContent>("test/data/3dR.mp4");
176         film->examine_and_add_content (R);
177         film->set_audio_channels(16);
178         BOOST_REQUIRE (!wait_for_jobs());
179
180         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
181         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
182
183         film->set_three_d (true);
184         make_and_verify_dcp (film);
185         check_dcp ("test/data/threed_test6", film->dir(film->dcp_name()));
186 }
187
188
189 /** Check 2D content set as being 3D; this should give an informative error */
190 BOOST_AUTO_TEST_CASE (threed_test7)
191 {
192         using boost::filesystem::path;
193
194         auto film = new_test_film2 ("threed_test7");
195         path const content_path = "test/data/flat_red.png";
196         auto c = content_factory(content_path)[0];
197         film->examine_and_add_content (c);
198         BOOST_REQUIRE (!wait_for_jobs());
199
200         c->video->set_frame_type (VideoFrameType::THREE_D);
201         c->video->set_length (24);
202
203         film->set_three_d (true);
204         make_dcp (film, TranscodeJob::ChangedBehaviour::IGNORE);
205         film->write_metadata ();
206
207         auto jm = JobManager::instance ();
208         while (jm->work_to_do ()) {
209                 while (signal_manager->ui_idle()) {}
210                 dcpomatic_sleep_seconds (1);
211         }
212
213         while (signal_manager->ui_idle ()) {}
214
215         BOOST_REQUIRE (jm->errors());
216         shared_ptr<Job> failed;
217         for (auto i: jm->_jobs) {
218                 if (i->finished_in_error()) {
219                         BOOST_REQUIRE (!failed);
220                         failed = i;
221                 }
222         }
223         BOOST_REQUIRE (failed);
224         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()));
225
226         while (signal_manager->ui_idle ()) {}
227
228         JobManager::drop ();
229 }
230
231
232 /** Trigger a -114 error by trying to make a 3D DCP out of two files with slightly
233  *  different lengths.
234  */
235 BOOST_AUTO_TEST_CASE (threed_test_separate_files_slightly_different_lengths)
236 {
237         auto film = new_test_film2("threed_test3");
238         auto L = make_shared<FFmpegContent>("test/data/test.mp4");
239         film->examine_and_add_content (L);
240         auto R = make_shared<FFmpegContent>("test/data/test.mp4");
241         film->examine_and_add_content (R);
242         BOOST_REQUIRE (!wait_for_jobs());
243
244         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
245         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
246         R->set_trim_end (dcpomatic::ContentTime::from_frames(1, 24));
247
248         film->set_three_d (true);
249         make_and_verify_dcp (film);
250 }
251
252
253 /** Trigger a -114 error by trying to make a 3D DCP out of two files with very
254  *  different lengths.
255  */
256 BOOST_AUTO_TEST_CASE (threed_test_separate_files_very_different_lengths)
257 {
258         auto film = new_test_film2("threed_test3");
259         auto L = make_shared<FFmpegContent>("test/data/test.mp4");
260         film->examine_and_add_content (L);
261         auto R = make_shared<FFmpegContent>("test/data/test.mp4");
262         film->examine_and_add_content (R);
263         BOOST_REQUIRE (!wait_for_jobs());
264
265         L->video->set_frame_type (VideoFrameType::THREE_D_LEFT);
266         R->video->set_frame_type (VideoFrameType::THREE_D_RIGHT);
267         R->set_trim_end (dcpomatic::ContentTime::from_seconds(1.5));
268
269         film->set_three_d (true);
270         make_and_verify_dcp (film);
271 }
272
273
274 BOOST_AUTO_TEST_CASE (threed_test_butler_overfill)
275 {
276         auto film = new_test_film2("threed_test_butler_overfill");
277         auto A = make_shared<FFmpegContent>(TestPaths::private_data() / "arrietty_JP-EN.mkv");
278         film->examine_and_add_content(A);
279         auto B = make_shared<FFmpegContent>(TestPaths::private_data() / "arrietty_JP-EN.mkv");
280         film->examine_and_add_content(B);
281         BOOST_REQUIRE (!wait_for_jobs());
282
283         Player player(film, Image::Alignment::COMPACT);
284         int const audio_channels = 2;
285         auto butler = std::make_shared<Butler>(
286                 film, player, AudioMapping(), audio_channels, boost::bind(PlayerVideo::force, AV_PIX_FMT_RGB24), VideoRange::FULL, Image::Alignment::PADDED, true, false, Butler::Audio::ENABLED
287                 );
288
289         int const audio_frames = 1920;
290         std::vector<float> audio(audio_frames * audio_channels);
291
292         B->video->set_frame_type(VideoFrameType::THREE_D_RIGHT);
293         B->set_position(film, dcpomatic::DCPTime());
294
295         butler->seek(dcpomatic::DCPTime(), true);
296         Butler::Error error;
297         for (auto i = 0; i < 960; ++i) {
298                 butler->get_video(Butler::Behaviour::BLOCKING, &error);
299                 butler->get_audio(Butler::Behaviour::BLOCKING, audio.data(), audio_frames);
300         }
301         BOOST_REQUIRE (error.code == Butler::Error::Code::NONE);
302 }
303
304
305 /** Check that creating a 2D DCP from a 3D DCP passes the J2K data unaltered */
306 BOOST_AUTO_TEST_CASE(threed_passthrough_test, * boost::unit_test::depends_on("threed_test6"))
307 {
308         using namespace boost::filesystem;
309
310         /* Find the DCP in threed_test6 */
311         boost::optional<path> input_dcp;
312         for (auto i: directory_iterator("build/test/threed_test6")) {
313                 if (is_directory(i.path()) && boost::algorithm::starts_with(i.path().filename().string(), "Dcp")) {
314                         input_dcp = i.path();
315                 }
316         }
317
318         BOOST_REQUIRE(input_dcp);
319
320         auto content = make_shared<DCPContent>(*input_dcp);
321         auto film = new_test_film2("threed_passthrough_test", { content });
322         film->set_three_d(false);
323
324         make_and_verify_dcp(film);
325
326         std::vector<directory_entry> matches;
327         std::copy_if(recursive_directory_iterator(*input_dcp), recursive_directory_iterator(), std::back_inserter(matches), [](directory_entry const& entry) {
328                 return boost::algorithm::starts_with(entry.path().filename().string(), "j2c");
329         });
330
331         BOOST_REQUIRE_EQUAL(matches.size(), 1U);
332
333         auto stereo = dcp::StereoPictureAsset(matches[0]);
334         auto stereo_reader = stereo.start_read();
335
336         auto mono = dcp::MonoPictureAsset(dcp_file(film, "j2c"));
337         auto mono_reader = mono.start_read();
338
339         BOOST_REQUIRE_EQUAL(stereo.intrinsic_duration(), mono.intrinsic_duration());
340
341         for (auto i = 0; i < stereo.intrinsic_duration(); ++i) {
342                 auto stereo_frame = stereo_reader->get_frame(i);
343                 auto mono_frame = mono_reader->get_frame(i);
344                 BOOST_REQUIRE(stereo_frame->left()->size() == mono_frame->size());
345                 BOOST_REQUIRE_EQUAL(memcmp(stereo_frame->left()->data(), mono_frame->data(), mono_frame->size()), 0);
346         }
347 }
348
349 /* #2476 was a writer error when 3D picture padding is needed */
350 BOOST_AUTO_TEST_CASE(threed_test_when_padding_needed)
351 {
352         auto left = content_factory("test/data/flat_red.png").front();
353         auto right = content_factory("test/data/flat_red.png").front();
354         auto sound = content_factory("test/data/sine_440.wav").front();
355         auto film = new_test_film2("threed_test_when_padding_needed", { left, right, sound });
356
357         left->video->set_frame_type(VideoFrameType::THREE_D_LEFT);
358         left->set_position(film, dcpomatic::DCPTime());
359         left->video->set_length(23);
360         right->video->set_frame_type(VideoFrameType::THREE_D_RIGHT);
361         right->set_position(film, dcpomatic::DCPTime());
362         right->video->set_frame_type(VideoFrameType::THREE_D_RIGHT);
363         right->video->set_length(23);
364         film->set_three_d(true);
365
366         make_and_verify_dcp(film);
367 }