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